Artikel ini menjelaskan kode terperinci dari pembacaan traversal Java dari konten file XML. Ini dibagikan kepada Anda untuk referensi Anda. Konten spesifiknya adalah sebagai berikut
tes paket; Impor java.io.fileInputStream; impor java.io.filenotfoundException; impor java.io.fileoutputstream; impor java.io.ioexception; impor java.io.outputStream; Impor Java.utilor; impor javax.xml.namespace.qonpace.qonx.maxonor; javax.xml.stream.xmlinputFactory; import javax.xml.stream.xmloutputFactory; import javax.xml.stream.xmlStreamException; impor javax.xml.stream.xmlamReader; impor javax.xml.streammmmmmmmmleamReam; org.apache.axiom.om.omabstractfactory; impor org.apache.axiom.om.omattribute; impor org.apache.axiom.om.omcomment; impor org.apache.axiom.omcontainer; impor org.apache.axiom.apache.axasource; org.apache.axiom.om.omdocument; impor org.apache.axiom.om.omelement; impor org.apache.axiom.om.omexception; impor org.apache.axiom.omfactory; import org.apache.axiom.omnamespace; import; org.apachex.apaxex.apaxex. 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) melempar filenotfoundException, throwable {// baca xml FileInputStream xmlfile = FileInputStream baru ("line-item2.xml"); XmlStreamReader parser = xmlinputFactory.newInstance (). CreatexmlStreamReader (xmlfile); // Objek Staxombuilder juga diperlukan pembangun Staxombuilder = Staxombuilder baru (parser); Omelement doc = builder.getDocumentElement (); // Baca <tool> </tool> Penyelidikan Cre = Doc.getFirstChildWithName (qname baru ("Siswa")); // Baca <Sahasiswa> Anymelement Cre1 = Cre.getFirstChildWithName (qname baru ("id")); // Baca <Dent> </dy> system.out.println (cre1.getLocalName ()+":"+cre1.getText ()); Cre1 = Cre.getFirstChildWithName (qname baru ("name")); // Baca <name> </name> System.out.println (cre1.getLocalName ()+":"+cre1.gettext ()); Cre1 = Cre.getFirstChildWithName (qname baru ("usia")); // Baca <AGE> </Eams> System.out.println (cre1.getLocalName ()+":"+cre1.getText ()); Cre1 = Cre.getFirstChildWithName (qname baru ("seks")); // Baca <sex> </pesen> System.out.println (cre1.getLocalName ()+":"+cre1.gettext ()); cre1 = cre.getFirstChildWithname (qname baru ("pesan")); // Baca <sex> </pesen> System.out.println (cre1.getLocalName ()+":"+cre1.gettext ()); cre1 = cre.getFirstChildWithname (qname baru ("pesan")); // Baca <sex> </pesen> 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 (); Node root di bawah ini omdocument dod = factory.createomdocument (); STU.AddChild (Factory.CreateomText ("Mac")); node ke node dod.addchild (root); FileInputStream ("2.xml"); doc1.getChildeLements (); System.out.println ("======================================================================================================================== = ITER1.NEXT (); OMF.Createomelement ("Name", "", "); Omelement Sex = OMF.Createomelement (" Sexy "," ","); OD.AddChild (root1);<?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 Dunia </sage> </dool>
Contoh Lain: Baca File XML dengan Java
Langkah -langkah untuk mengurai XML adalah sebagai berikut:
Paket yang Digunakan:
Objek yang digunakan:
Metode yang digunakan:
Mari kita menguraikan file XML
impor javax.xml.parsers.*; impor org.w3c.dom.*; impor org.xml.sax.*; tes kelas publik {public static void main (string [] args) {DocumentBuilderFactory dbf = DocumentBuilderFactory.NewInstance (); coba {DocumentBuilder db = dbf.newDocumentBuilder (); Dokumen dokumen = db.parse ("pet2.xml"); Nodelist doglist = doc.getElementsbyTagname ("dog"); System.out.println ("Total" + Doglist.getLength () + "Node Dog"); untuk (int i = 0; i <doglist.getLength (); i ++) {node dog = doglist.item (i); Elemen elem = (elemen) anjing; System.out.println ("id:" + elem.getAttribute ("id")); untuk (node node = dog.getFirstChild (); node! = null; node = node.getNextSibling ()) {if (node.getNodetype () == node.element_node) {string name = node.getNodename (); Nilai string = node.getFirstChild (). GetNodevalue (); System.out.print (name + ":" + value + "/t"); }} System.out.println (); }} catch (Exception e) {e.printstacktrace (); }}}File xml
<Pets> <dog> <dog id = "1"> <name> yaya </name> <Health> 100 </health> <e love> 0 </love> <strain> keren shinnari </strain> </dog> <dog id = "2"> <name> ouou </name> </health> 90 </health> <love> 15 "</name> labor <Penguin ID = "3"> <name> qq </name> <alth> 100 </health> <love> 20 </love> <ap sex> qzi </sex> </penguin> </sets> </pets>
Di atas adalah semua tentang artikel ini, saya harap ini akan membantu untuk pembelajaran semua orang.