이 기사에서는 XML 파일의 내용에 대한 Java Traversal 읽기의 자세한 코드를 설명합니다. 참조를 위해 귀하와 공유됩니다. 특정 내용은 다음과 같습니다
패키지 테스트; 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; javax.xml.namespace.qname; import javax.xml.xml.xmlinputory; import javax.xml.stream.xmloutputfactory; import javax.xml.stream.xmlstreamexception; import javax.xml.stream.xmltreamreader; import javax.xml.xml.xml.sml.sml.sml.xml.xml.xml.xml.xml.xml.xml org.apache.axiom.om.omabstractfactory; import org.apache.axiom.om.omattribute; import org.apache.axiom.om.om.om.om.om.om.om.om.om.om.om.omcontainer; import org.apache.ax.om.om.omdatasource; import org.ax.axiom.om.om.omdocte; org.apache.axiom.om.om.omdocument; import org.apache.axiom.om.omelement; import org.apache.axiom.om.om.om.om.om.om.om.om.om.om.om.om.om.om.omexception; import org.apache.axiom.om.omnamespace; import org.apache.axiom.om.omprocessingtruction; org.apache.axiom.om.omsourcedelement; import org.apache.axiom.om.omtext; import org.apache.axiom.om.omxmlparserwrapper; import org.apache.axiom.om.impl.builder.builder.staxombuilder; import org.xml.sax.helpers.xmlerformory; public class axiomtest {public static void main (string [] args)은 filenotfoundException을 던지고 던질 수있는 {// xml fileInputStream xmlfile = new FileInputStream ( "line-item2.xml"); xmlstreamreader parser = xmlinputfactory.newinstance (). createxmlstreamreader (xmlfile); // Staxombuilder 객체는 또한 필요한 staxombuilder builder = new Staxombuilder (Parser); Omlement doc = builder.getDocumentElement (); // 읽기 <바보> </gel> omlement cre = doc.getFirstChildWithName (new Qname ( "Student")); // <tudent> omlement cre1 = cre.getfirstchildwithName (new Qname ( "id")); // <id> </id> system.out.println (cre1.getLocalName ()+":"+cre1.getText ()); cre1 = cre.getfirstchildwithName (new Qname ( "name")); // <name> </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> system.out.println (cre1.getLocalName ()+":"+cre1.getText ()); cre1 = cre.getfirstchildwithName (new Qname ( "Message")); // <섹스> </sex> system.out.println (cre1.getLocalName ()+":"+cre1.getText ()); cre1 = cre.getfirstchildwithName (new Qname ( "Message")); // <섹스> </sex> system.out.println (cre1.getLocalName ()+":"+cre1.getText ()); System.out.println("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- system.out.println (temp.getText ()); System.out.println("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------. System.out.println ( "!!!!!!!!!!!!!!! sta.getDocumentElement (); (ite2.hasnext ()) {omlement temp1 = intem2.next (); omdocument dod = factory.createomdocument (); root node omlement root.createomelement ( "root", ","); stu.addchild ( "Mac"); 루트 노드를 dod.addchild (루트)로 연결합니다. // 출력 장치 xmlStreamwriter 새로운 FileInputStream ( "2.xml"); doc1.getChildElements (); System.out.println ( "================================================================================================= = iter1.next.out.println ( "!!!!!"; OMF. Od.AddChild (root1) xmlStreamwriter = xmloutputfactory ().<? xml version = "1.0"alcoding = "utf-8"?> <gely> <tudent> <name> mac </name> <id> 12 </id> </id> age> </age> <sex> male </sex> <메시지> Hello World </world> </student> <student> <name> </sehent> <age </age </seel> </age </seel> <이름> MR. JONES </name> <id> 2 </id> <ge> 31 </age> <ege> <sect> male </sex> </el 세계 </메시지> </gel>
또 다른 예 : Java로 XML 파일을 읽으십시오
XML을 구문 분석하는 단계는 다음과 같습니다.
사용 된 패키지 :
사용 된 개체 :
사용 된 방법 :
XML 파일을 구문 분석하겠습니다
import javax.xml.parsers.*; import org.w3c.dom.*; import 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 ( "Dog"); System.out.println ( "Total" + doglist.getLength () + "Dog Nodes"); for (int i = 0; i <doglist.getLength (); i ++) {node dog = doglist.item (i); 요소 elem = (요소) 개; System.out.println ( "id :" + elem.getAttribute ( "id")); for (node = dog.getFirstChild (); node! = null; node = node.getNexTibling ()) {if (node.getNodeType () == node.element_node) {String name = node.getNodename (); 문자열 값 = node.getFirstChild (). getNodevalue (); System.out.print (이름 + ":" + value + "/t"); }} system.out.println (); }} catch (예외 e) {e.printstacktrace (); }}}XML 파일
<pets> <dogs> <dog id = "1"> <name> yaya </name> <emply> 100 </health> <love> 0 </love> <lain> cool shinnari </warty> </dog> <dog id = "2"> <name> ouou </name> <ecty> 90 </health> <love> <smart labrador </smart> </fur>> <penguin id = "3"> <name> qq </name> <emply> 100 </health> <love> 20 </love> <sex> qzi </sex> </penguin> </pets> </pets>
위의 내용은이 기사에 관한 모든 것입니다. 모든 사람의 학습에 도움이되기를 바랍니다.