This article explains the detailed code of java traversal reading of the content of xml file. It is shared with you for your reference. The specific content is as follows
package test; import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.Iterator;import javax.xml.namespace.NamespaceContext;import javax.xml.namespace.QName;import javax.xml.stream.XMLInputFactory;import javax.xml.stream.XMLOutputFactory;import javax.xml.stream.XMLStreamException;import javax.xml.stream.XMLStreamReader;import javax.xml.stream.XMLStreamWriter;import org.apache.axiom.om.OMAbstractFactory;import org.apache.axiom.om.OMAttribute;import org.apache.axiom.om.OMComment;import org.apache.axiom.om.OMContainer;import org.apache.axiom.om.OMDataSource;import org.apache.axiom.om.OMDocType;import org.apache.axiom.om.OMDocument;import org.apache.axiom.om.OMElement;import org.apache.axiom.om.OMException;import org.apache.axiom.om.OMFactory;import org.apache.axiom.om.OMNamespace;import org.apache.axiom.om.OMProcessingInstruction;import org.apache.axiom.om.OMSourcedElement;import org.apache.axiom.om.OMText;import org.apache.axiom.om.OMXMLParserWrapper;import org.apache.axiom.om.impl.builder.StAXOMBuilder;import org.xml.sax.helpers.XMLReaderFactory; public class Axiomtest { public static void main(String[] args) throws FileNotFoundException, Throwable {// read xml FileInputStream xmlFile = new FileInputStream("line-item2.xml"); XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(xmlFile); // StAXOMBuilder object is also required StAXOMBuilder builder = new StAXOMBuilder(parser); OMElement doc = builder.getDocumentElement(); // Read <fool></fool> OMElement cre = doc.getFirstChildWithName(new QName("student")); //Read <student> OMElement cre1 = cre.getFirstChildWithName(new QName("id")); //Read <id></id> System.out.println(cre1.getLocalName()+":"+cre1.getText()); cre1 = cre.getFirstChildWithName(new QName("name")); //Read <name></name> System.out.println(cre1.getLocalName()+":"+cre1.getText()); cre1 = cre.getFirstChildWithName(new QName("age")); // Read <age></age> System.out.println(cre1.getLocalName()+":"+cre1.getText()); cre1 = cre.getFirstChildWithName(new QName("sex")); // Read <sex></sex> System.out.println(cre1.getLocalName()+":"+cre1.getText()); cre1 = cre.getFirstChildWithName(new QName("message")); // Read <sex></sex> System.out.println(cre1.getLocalName()+":"+cre1.getText()); cre1 = cre.getFirstChildWithName(new QName("message")); // Read <sex></sex> System.out.println(cre1.getLocalName()+":"+cre1.getText()); System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ System.out.println(temp.getText()); if(temp.getLocalName().equals("student")){ Iterator<OMElement> iter1 = temp.getChildElements(); System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- System.out.println("!!!!!!!!!!!!!!!!!!"); FileInputStream file = new FileInputStream("line-item2.xml"); XMLStreamReader read = XMLInputFactory.newInstance().createXMLStreamReader(file); StAXOMBuilder sta = new StAXOMBuilder(read); OMElement all = sta.getDocumentElement(); Iterator<OMElement> item1 = all.getChildElements(); while(ite1.hasNext()){ OMElement temp = item1.next(); if(temp.getLocalName().equals("student")){ Iterator<OMElement> item2 = temp.getChildElements(); while(ite2.hasNext()){ OMElement temp1 = item2.next(); System.out.println(temp1.getLocalName()+":"+temp1.getText()); } } } // write xml OMFactory factory = OMAbstractFactory.getOMFactory(); //Create a doc node, and the doc node will merge with the root node below OMDocument dod = factory.createOMDocument(); //Create a root node OMElement root = factory.createOMElement("root","","); OMElement add = factory.createOMElement("dabi","","); //Create two ordinary nodes OMElement stu = factory.createOMElement("student","","); stu.addChild(factory.createOMText("mac")); OMElement tea = factory.createOMElement("teacher","",""); tea.addChild(factory.createOMText("silly")); //Build the tree, connect two ordinary nodes to the root node root.addChild(stu); root.addChild(tea); //Build the tree, connect the root node to the doc node dod.addChild(root); //Build the writer as the output device XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter( new FileOutputStream("2.xml")); root.serialize(writer); // cache on writer.flush(); FileInputStream xmlFile1 = new FileInputStream("2.xml"); XMLStreamReader parser1 = XMLInputFactory.newInstance().createXMLStreamReader(xmlFile1); StAXOMBuilder builder1 = new StAXOMBuilder(parser1); OMElement doc1 = builder1.getDocumentElement(); Iterator<OMElement> iter1 = doc1.getChildElements(); while(iter1.hasNext()){ OMElement temp = iter1.next(); System.out.println("======================================================================================== while(iter1.hasNext()){ OMElement temp = iter1.next(); System.out.println("!!!!!!"); OMFactory omf = OMAbstractFactory.getOMFactory();// OMDocument od = omf.createOMDocument(); OMElement root1 = omf.createOMElement("root","",""); OMElement name = omf.createOMElement("name","","); OMElement sex = omf.createOMElement("sexy","","); sex.addChild(omf.createOMText("man")); name.addChild(omf.createOMText("dabi")); root1.addChild(sex); root1.addChild(name);// od.addChild(root1); XMLStreamWriter xmlw = XMLOutputFactory.newInstance().createXMLStreamWriter(new FileOutputStream("3.xml")); root1.serialize(xmlw); xmlw.flush(); }}<?xml version="1.0" encoding="UTF-8"?><fool> <student> <name>mac</name> <id>12</id> <age>33</age> <sex>male</sex> <message>hello world</message> </student> <student> <name>silly</name> <id>5</id> <age>12</age> <sex>female</sex> </student> <teacher> <name>Mr. Jones</name> <id>2</id> <age>31</age> <sex>male</sex> </teacher> <student> <name>macy</name> <id>2</id> <age>40</age> <sex>female</sex> </student> <student> <name>tom</name> <id>32</id> <age>31</age> <sex>male</sex> </student> <message>hello world</message></fool>
Another example: Read XML files with JAVA
The steps to parse XML are as follows:
Packages used:
Objects used:
Methods used:
Let's parse an XML file
import javax.xml.parsers.*; import org.w3c.dom.*; import org.xml.sax.*; public class Test { public static void main(String[] args) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse("pet2.xml"); NodeList dogList = doc.getElementsByTagName("dog"); System.out.println("Total" + dogList.getLength() + "dog nodes"); for (int i = 0; i < dogList.getLength(); i++) { Node dog = dogList.item(i); Element elem = (Element) dog; System.out.println("id:" + elem.getAttribute("id")); for (Node node = dog.getFirstChild(); node != null; node = node.getNextSibling()) { if (node.getNodeType() == Node.ELEMENT_NODE) { String name = node.getNodeName(); String value = node.getFirstChild().getNodeValue(); System.out.print(name + ":" + value + "/t"); } } System.out.println(); } } catch (Exception e) { e.printStackTrace(); } } }XML files
<pets> <dogs> <dog id="1"> <name>YAYA</name> <health>100</health> <love>0</love> <strain>Cool Shinnari</strain> </dog> <dog id="2"> <name>OUOU</name> <health>90</health> <love>15</love> <strain>Smart Labrador</strain> </dog> </dogs> <penguins> <penguin id="3"> <name>QQ</name> <health>100</health> <love>20</love> <sex>Qzi</sex> </penguin> </pets> </pets>
The above is all about this article, I hope it will be helpful to everyone's learning.