この記事では、XMLファイルのコンテンツのJavaトラバーサル読み取りの詳細なコードについて説明します。それはあなたの参照のためにあなたと共有されます。特定のコンテンツは次のとおりです
パッケージテスト; Import java.io.fileinputStream; Import Java.io.fileNotFoundException; Import java.io.fileOutputStream; Import java.io.ioexception; Import java.utputStream; Import java.util.iterator; Import javax.xml.names.namespacecontext; Import; javax.xml.namespace.qname; import javax.xml.stream.xmlinputfactory; import javax.xml.stream.xmloutputfactory; import javax.xml.Stream; Import javax.xml.xmlstreamexception; Import javax.xml.Stream.XmlStreader apache.axiom.om.omabstractfactory; Import org.apache.axiom.om.omattribute; Import org.apache.axiom.om.om.omcomment; Import org.apache.axiom.om.om.omcontainer; Import org.apache.axiom.om.om.om.om.om.om.om.om.om.om.om.om.om.om.om.om.om.om.om.om.om.om.om.om.om.om.om.om.om. org.apache.axiom.om.omdocument; Import org.apache.axiom.om.om.om.om.om.omelement; Import org.apache.axiom.om.om.omexception; Import org.apache.axiom.om.om.om.omactory; Import org.apache.axiom.om.om.omnamespace; Import org.apache.axiom.axiom.om.om.omprocessinging org.apache.axiom.om.omsourcedelement; import org.apache.axiom.om.omtext; Import org.apache.axiom.om.om.omxmlparserwrapper; Import org.apache.axiom.om.impl.builder.staxombuilder; Import org.xml.xml.sax.sax.sacd.sicd.xml.sicax.sack.sumpers. public class axiomtest {public static void main(string [] args)throws filenotfoundexception、throwable {// xml fileinputStream xmlfile = new fileinputStream( "line-item2.xml"); xmlstreamreader parser = xmlinputfactory.newinstance()。createxmlstreamreader(xmlfile); // staxOmbuilderオブジェクトも必要ですstaxombuilderビルダー= new staxombuilder(パーサー); Omelement doc = builder.getDocumentElement(); // <fool> </fool> Omelement cre = doc.getFirstChildWithName(new QName( "desute")); // <Student> Omelement 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> </sex> system.out.println(cre1.getLocalName()+":"+cre1.getText())を読むcre1 = cre.getFirstChildWithName(new QName( "Message")); // <sex> </sex> system.out.println(cre1.getLocalName()+":"+cre1.getText())を読むcre1 = cre.getFirstChildWithName(new QName( "Message")); // <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(); (ite2.hasnext()){omelement temp1 = itele2.out.println()+":"+temp1.getText()} Omdocument dod = factory.createomdocument(); Stu.AddChild( "Mac");ルートノードは、dod.addchild(root)に接続しますFileInputStream( "2.xml"); doc1.getChildElements(); System.out.println( "================================================================================= = iter1.next(); omf.createomelement( ""、 "); omf.createomelement(" "、"); Od.AddChild(root1);<?xml version = "1.0" encoding = "utf-8"?> <fool> <sustent> <name> mac </name> <id> 12 </id> <age> 33 </age> <sex> male </sex> <message> hello world </message> ch <NAME> MR。ジョーンズ</name> <id> 2 </id> <age> 31 </age> <sex>男性</sex> </teacher> <sudent> <name> macy </name> <id> 2 </id> <年齢> 40 </age> <sex> world </message> </fool>
別の例: Javaを使用してXMLファイルを読み取ります
XMLを解析する手順は次のとおりです。
使用されるパッケージ:
使用されるオブジェクト:
使用する方法:
XMLファイルを解析しましょう
javax.xml.parsers。*; org.w3c.domをインポート。*; 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);要素elem =(要素)犬; System.out.println( "id:" + elem.getattribute( "id")); for(node node = dog.getFirstChild(); node!= node = 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(例外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"> ouou </name> <health> 90 </heため<Penguins> <Penguin id = "3"> <name> qq </name> <health> 100 </health> <love> 20 </love> <sex> qzi </sex> </penguin> </pets> </pets> <
上記はこの記事に関するものです。すべての人の学習に役立つことを願っています。