मेरे पास एक 4 एक्सएमएल फाइलें हैं जिनकी संरचना निम्नानुसार है:
<?xml version="1.0"?>
<Registry>
<Insitutions>
<Insitiution>
<name>A</name>
<location>B</location>
</Insitiution>
<Insitiution>
<name>C</name>
<location>D</location>
</Insitiution>
<Insitiution>
<name>E</name>
<location>F</location>
</Insitiution>
</Insitutions>
</Registry>
मैंने अलग-अलग एक्सएमएल टैग के लिए कक्षाएं लिखी हैं, और जैक्सबी का उपयोग करके उन्हें अनारक्षित किया है। मैं सभी फाइलों को अनमर्शल करता हूं और सभी प्रविष्टियों को एक सूची में निम्नानुसार संग्रहीत करता हूं:
private static List<String> loadBaseNodesets() throws JAXBException {
log.info("Loading nodesets");
Registry xmlentries = null;
JAXBContext jaxbContext = null;
List<Registry> entries = new ArrayList<>();
List<String> privateBaseNodeSets= new ArrayList<>();
File dir = new File("XMLFiles");
if (dir.exists() && dir.isDirectory()) {
FileFilter filter = new FileFilter() {
public boolean accept(File file) {
return file.isFile() && file.getName().endsWith(".xml");
}
};
log.info("iterating through all files");
File [] files = dir.listFiles(filter);
if (files != null) {
for (int i =0;i <1;i++) {
jaxbContext = JAXBContext.newInstance(Registry.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
xmlentries = (Registry) jaxbUnmarshaller.unmarshal(files[i]);
privateBaseNodeSets.add(xmlentries.toString());
log.info(files[i].getName()+" NodeSet loaded");
entries.add(xmlentries);
}
}
}
log.info("Nodeset loading complete");
return privateBaseNodeSets;
}
मुझे सभी xmlentries को स्ट्रिंग की सूची में रखना होगा।
अब मुख्य कार्यक्रम से, मैं यह जांचना चाहूंगा कि क्या मुझे XML प्रविष्टियों का समान प्रारूप मिल सकता है।
public static void main(String[] args) throws JAXBException {
log.info("Started local test main");
List<String> baseNodesets = loadBaseNodesets();
ListIterator<String> litr = baseNodesets.listIterator();
while (litr.hasNext()) {
JAXBContext jaxbContext=JAXBContext.newInstance(Registry.class);
Marshaller marshaller = jaxbContext.createMarshaller();#
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(, System.out);// DONT KNOW THE FIRST PARAMETER
}
मैं यहां पहला पैरामीटर प्राप्त करने में सक्षम नहीं हूं। क्या कोई कृपया मदद कर सकता है?
2 जवाब
मुझे यकीन नहीं है कि मैं आपके प्रश्न को सही ढंग से समझ गया हूं, लेकिन मुझे लगता है कि आप ऐसा कुछ करने की कोशिश कर रहे हैं:
- प्रत्येक फ़ाइल को एक-एक करके पढ़ें और
entries
सूची में अनमर्शल और स्टोर करें। - फिर
entries List
पर लूप करें और उन्हें एक-एक करके मार्शल करें। - परिवर्तित एक्सएमएल को सूची में स्टोर करें
मैंने एक ही फ़ोल्डर में 2 समान XML फ़ाइलें संग्रहीत कीं और इसे पढ़ने का प्रयास किया
एक्सएमएल फाइलें:
<?xml version="1.0"?>
<Registry>
<Insitutions>
<Insitiution>
<name>A</name>
<location>B</location>
</Insitiution>
<Insitiution>
<name>C</name>
<location>D</location>
</Insitiution>
<Insitiution>
<name>E</name>
<location>F</location>
</Insitiution>
</Insitutions>
</Registry>
जड़:
@Data
@XmlRootElement(name = "Registry")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
@XmlElementWrapper(name = "Insitutions")
@XmlElement(name = "Insitiution")
private List<Insitiution> Insitiution;
}
संस्थान:
@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class Insitiution {
private String name;
private String location;
}
मार्शलिंग (मुख्य):
public class Marshalling {
public static void main(String[] args) throws JAXBException, XMLStreamException, FactoryConfigurationError, IOException, SAXException {
final File dir = new File("/Users/Downloads/test");
final List<Root> entries = new ArrayList<>();
final Unmarshaller unmarshaller = JAXBContext.newInstance(com.newJAXB.Root.class).createUnmarshaller();
final StringWriter singleXmlEvent = new StringWriter();
final List<String> xmlOutput = new ArrayList<>();
final Marshaller marshaller = JAXBContext.newInstance(Root.class).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
for (File file : dir.listFiles()) {
if (!file.isDirectory() && file.getName().endsWith(".xml")) {
//Storing all the files into entries List
final DataInputStream inputStream = new DataInputStream(new FileInputStream(file.getAbsolutePath()));
final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
final com.newJAXB.Root root = unmarshaller.unmarshal(xmlStreamReader, com.newJAXB.Root.class).getValue();
entries.add(root);
System.out.println(root.toString());
}
}
//Loop through each entry in entries and marshal it
for (Root item : entries) {
marshaller.marshal(item, singleXmlEvent);
xmlOutput.add(singleXmlEvent.toString());
// Clear the StringWriter for next event
singleXmlEvent.getBuffer().setLength(0);
}
//Final all the XML
System.out.println(xmlOutput);
}
}
आउटपुट:
Root(Insitiution=[Insitiution(name=A, location=B), Insitiution(name=C, location=D), Insitiution(name=E, location=F)])
Root(Insitiution=[Insitiution(name=A, location=B), Insitiution(name=C, location=D), Insitiution(name=E, location=F)])
[<?xml version="1.0" encoding="UTF-8"?>
<Registry>
<Insitutions>
<Insitiution>
<name>A</name>
<location>B</location>
</Insitiution>
<Insitiution>
<name>C</name>
<location>D</location>
</Insitiution>
<Insitiution>
<name>E</name>
<location>F</location>
</Insitiution>
</Insitutions>
</Registry>
, <?xml version="1.0" encoding="UTF-8"?>
<Registry>
<Insitutions>
<Insitiution>
<name>A</name>
<location>B</location>
</Insitiution>
<Insitiution>
<name>C</name>
<location>D</location>
</Insitiution>
<Insitiution>
<name>E</name>
<location>F</location>
</Insitiution>
</Insitutions>
</Registry>
]
आपका तरीका मार्शल को पहले पैरामीटर के रूप में XML में बदलने के लिए जावा ऑब्जेक्ट की आवश्यकता है।
यह आपकी प्रविष्टियों की सूची को xml की सूची में बदल देगा।
final JAXBContext jaxbContext=JAXBContext.newInstance(Registry.class);
final Marshaller marshaller = jaxbContext.createMarshaller();
List<String> xmlEntries = entries.stream().map(entrie -> {
OutputStream os = new ByteArrayOutputStream();
marshaller.marshal(entrie, os); <-- your object to transform to XML
return os.toString();
}.collect(Collectors.toList());
संबंधित सवाल
जुड़े हुए प्रश्न
नए सवाल
java
जावा एक उच्च स्तरीय प्रोग्रामिंग भाषा है। इस टैग का उपयोग तब करें जब आपको भाषा का उपयोग करने या समझने में समस्या हो। इस टैग का उपयोग शायद ही कभी किया जाता है और इसका उपयोग अक्सर [वसंत], [वसंत-बूट], [जकार्ता-ई], [Android], [javafx], [हडूप], [श्रेणी] और [मावेन] के साथ किया जाता है।