В этой статье объясняется подробный код чтения Java Traversal содержимого XML -файла. Это совместно с вами для вашей ссылки. Конкретный контент заключается в следующем
Пакет -тест; Импорт java.io.fileinputstream; import java.io.filenotfoundexception; импорт java.io.fileOutputStream; импорт java.io.ioexception; импорт java.io.outputstream; import java.util.iterator; импорт javax.xml.namespace.namespacecontext; javax.xml.namespace.qname; import javax.xml.stream.xmlinputfactory; import javax.xml.stream.xmloutputfactory; import javax.xml.stream.xmlstreamexception; import javax.xml.Stream.xmleAm.momporteAm.xmleam. org.apache.axiom.om.omabstractfactory; import org.apache.axiom.om.omattribute; импорт org.apache.axiom.om.omment; импорт org.apache.axiom.om.omcontainer; импорт org.apache.axiom.om.omdatasource; import org.apache.am.om 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.omsourceedelement; import org.apache.axiom.om.omext; импорт org.apache.axiom.om.omxmlparserwrapper; import org.apache.axiom.om.impl.builder.staxombuilder; import.xml.sax.helpers.xlabrableder; import.xml.sax.helpers.xmlabrablider; import.sax.Hel открытый класс axiomtest {public static void main (string [] args) бросает filenotfoundexception, throwable {// read xml fileInputStream xmlfile = new FileInputStream ("line-item2.xml"); XmlstreamReader parser = xmlinputfactory.newinstance (). CreatexmlStreamReader (xmlfile); // Объект StaxMobuilder также требуется StaxBombuilder Builder = New StaxMombuilder (анализатор); Omelement doc = builder.getDocumentElement (); // Читать <дурак> </pourt> omelement cre = doc.getfirstchildwithname (new qname ("студент")); // Читать <tlood> omelement cre1 = cre.getfirstchildwithname (new qname ("id")); // Читать <id> </id> system.out.println (cre1.getlocalname ()+":"+cre1.getText ()); cre1 = cre.getfirstchildwithName (new qname ("name")); // Читать <mame> </name> system.out.println (cre1.getlocalname ()+":"+cre1.getText ()); cre1 = cre.getfirstchildwithName (new qname ("age")); // Читать <Age> </age> System.out.println (cre1.getLocalName ()+":"+cre1.getText ()); cre1 = cre.getfirstchildwithname (new qname ("sex")); // Читать <Sex> </sex> system.out.println (cre1.getlocalname ()+":"+cre1.getText ()); cre1 = cre.getfirstchildwithName (new qname ("Сообщение")); // Читать <Sex> </sex> system.out.println (cre1.getlocalname ()+":"+cre1.getText ()); cre1 = cre.getfirstchildwithName (new qname ("Сообщение")); // Читать <Sex> </sex> system.out.println (cre1.getlocalname ()+":"+cre1.getText ()); System.out.println ("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- System.out.println (temp.getText ()); System.out.println ("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- System.out.println ("!!!!!!!!!!!!!!! sta.getDocumentElement (); while (ite2.hasnext ()) {omelement temp1 = item2.next (); Omdocument dod = factory.createomDocument (); Stu.addchild (Factory.createomtext ("Mac"); root node to Doc node dod.addchild (root); FileInputStream ("2.xml"); doc1.getChildElements (); System.out.println ("====================================================================================================================== iter1.next (); Omelement sex = omf.createomelement ("sexy", "", "); sex.addchild (omf.createomtext (" man ")); name.addchild (omf.createomtext (" dabi ")); root1.addchild (sex); root1.addchild (" dabi "); root1.addchild (sex); root1.addchild (name); Xmloutputfactory.newinstance ().<? xml version = "1.0" Encoding = "UTF-8"?> <Uppl> <doupt> <mame> mac </name> <id> 12 </id> <возраст> 33 </Возраст> <ses> Мужчина </sex> <Сообщение> Hello World </message> </student> <bolder> <mame> </name> <id> 5 </id> <aster> </age> </age> </id> </id> </id> <aster> </age> </id> </id> <id> <id>. <mame> 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> </bool>
Другой пример: прочитайте файлы XML с Java
Шаги по анализу XML следующие:
Используются пакеты:
Используются объекты:
Используются методы:
Давайте проанализируем файл XML
Импорт javax.xml.parsers.*; Импорт org.w3c.dom.*; Импорт org.xml.sax.*; Общественный тест класса {public static void main (string [] args) {documentBuilderFactory dbf = documentBuilderFactory.newinStance (); try {documentBuilder db = dbf.newdocumentBuilder (); Документ doc = db.parse ("pet2.xml"); Nodelist Doglist = doc.GetElementsBytagName ("собака"); System.out.println ("total" + doglist.getLength () + "собачьи узлы"); for (int i = 0; i <doglist.getLength (); i ++) {node dog = doglist.item (i); Элемент elem = (элемент) собака; System.out.println ("id:" + elem.getattribute ("id")); for (node node = dog.getfirstchild (); node! = null; node = node.getNextIbling ()) {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 -файлы
<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"> <mame> QQ </name> <Health> 100 </Health> <Love> 20 </love> <ses> qzi </sex> </penguin> </pets> </pets>
Выше приведено в этой статье, я надеюсь, что это будет полезно для каждого обучения.