Este artigo explica o código detalhado da leitura de travessia Java do conteúdo do arquivo XML. É compartilhado com você para sua referência. O conteúdo específico é o seguinte
teste de pacote; importar java.io.fileInputStream; importar java.io.filenotfoundException; importar java.io.fileOutputStream; importar java.io.ioexception; importar java.io.outputStream; import java.util.iterator; import javax.xml.namespace.namespacon; javax.xml.namespace.qname; importar javax.xml.stream.xmlinputFactory; importar javax.xml.stream.xmloutputFactory; import javax.xml.stream.xmlStreamException; import javax.ml.stream.xml.streamader; org.apache.axiom.om.omabStractFactory; importar org.apache.axiom.om.omattribute; importar org.apache.axiom.om.omcomment; importação org.apache.axiom.omcontainer; import org.apache.axiom.omaxaxaMOMOMOM.MOMOMOPAPAING; org.apache.axiom.om.omdocument; importar org.apache.axiom.om.omElement; importar org.apache.axiom.om.omexception; importação org.apache.axiom.omation.omfactory; importache.apache.apache.om.om.om.omocace; import.apactory; 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; classe pública axiomtest {public static void main (string [] args) lança fileNotfoundException, throwable {// leia xml fileInputStream xmlfile = new FileInputStream ("line-item2.xml"); XMLStreamReader Parser = xmlinputFactory.NewInstance (). CreatexmlStreamReader (xmlfile); // objeto Staxombuilder também é necessário Staxombuilder Builder = new Staxombuilder (Parsers); Omelement doc = builder.getDocumentElement (); // leia <Oole> </ool> omelement cre = doc.getfirstchildWithName (novo QNAME ("Student")); // leia <denty> omelement cre1 = cre.getfirstchildWithName (novo QNAME ("id")); // leia <id> </id> system.out.println (cre1.getLocalName ()+":"+cre1.getText ()); Cre1 = cre.getfirstChildWithName (novo QNAME ("nome")); // leia <name> </name> System.out.println (cre1.getLocalName ()+":"+cre1.getText ()); Cre1 = cre.getfirstchildWithName (novo QNAME ("Age")); // leia <age> </age> System.out.println (cre1.getLocalName ()+":"+cre1.getText ()); Cre1 = cre.getfirstchildWithName (novo QNAME ("sexo")); // leia <sex> </xex> System.out.println (cre1.getLocalName ()+":"+cre1.getText ()); Cre1 = cre.getfirstChildWithName (novo QNAME ("message")); // leia <sex> </xex> System.out.println (cre1.getLocalName ()+":"+cre1.getText ()); Cre1 = cre.getfirstChildWithName (novo QNAME ("message")); // leia <sex> </xex> System.out.println (cre1.getLocalName ()+":"+cre1.getText ()); System.Out.println ("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- System.out.println (temp.getText ()); System.out.println ("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ System.Println ("!!!!!!!!!!!!!!! sta.getDocumentElement (); while (ITE2.hasnext ()) {omelement temp1 = item2.next (); Abaixo do documento omdocument = Factory.createomdocument (); Stu.Addchild (Factory.Createomtext ("Mac"); Nó para o Doc Node DOD.Addchild (Root); FileInputStream ("2.xml"); doc1.getChildElements (); System.out.println ("====================================================================================================================================================================== 1. = iter1.next (); OMELEMENTO SEX = OMF.CREATEOMELEMENT ("SEXY", "", "); sex.addchild (OMF.CreateomText (" Man "); name.addchild (omf.createomText (" dabi ")); root1.addchild (sexo); root1.addCild (name); XmloutputFactory.NewInstance ().<? xml versão = "1.0" coding = "utf-8"?> <ilog> <denty> <name> mac </name> <d> 12 </d> <eane> 33 </ge> <ogem> masculino </math> <dssion> hello world </mession> </udent> student> <name> silly </mat> <d> 5 </id> 5 </dent> </idan> </id> </sghty> </mssion> </student> <name> silly </sexo> <d> 5 </id> 5 </d. <nome> sr. Jones </name> <id> 2 </d> <age> 31 </age> <ogem> masculino </sex> </sestion> <dent> <name> macy </name> <id> 2 </dod> <ege> 40 </idade <DELE> MEMOL </IDSE </SEX> </SELE> </SUSTE> <name> </nome> <del> 9 31 31 32 </sel> MUNDO </ssents> </ool>
Outro exemplo: leia arquivos XML com java
As etapas para analisar XML são as seguintes:
Pacotes usados:
Objetos usados:
Métodos usados:
Vamos analisar um arquivo XML
importar javax.xml.parsers.*; importar org.w3c.dom.*; importar org.xml.sax.*; public class Test {public static void main (string [] args) {documentBuilderFactory dbf = documentBuilderFactory.NewInstance (); tente {documentBuilder db = dbf.newdocumentBuilder (); Documento doc = db.parse ("pet2.xml"); Nodelist DogList = doc.getElementsByTagName ("cachorro"); System.out.println ("total" + dogList.getLength () + "nós de cachorro"); for (int i = 0; i <dogList.getLength (); i ++) {node Dog = DogList.item (i); Elemento elem = (elemento) cão; System.out.println ("id:" + elem.getAttribute ("id")); para (nó nó = cachorro.getfirstchild (); node! = null; node = node.getNextsBling ()) {if (node.getNodEType () == Node.Element_Node) {string name = node.getNodename (); String value = node.getfirstchild (). GetNodEvalue (); System.out.print (nome + ":" + valor + "/t"); }} System.out.println (); }} catch (Exceção e) {e.printStackTrace (); }}}Arquivos XML
<Pets> <dogs> <cão id = "1"> <name> yaya </name> <saúde> 100 </ail Health> <lorge> 0 </ro love> <firse> Cool Shinnari </s tender> </gog> <dog id = "2"> <name> Ouou </name> <lecking> 90 <//Health> </Love </Love> <Penguin id = "3"> <name> qq </name> <saúde> 100 </aility> <ling> 20 </ro love> <og> qzi </sex> </singuin> </pets> </sets>
O exposto acima é tudo sobre este artigo, espero que seja útil para o aprendizado de todos.