Este artículo explica el código detallado de la lectura transversal de Java del contenido del archivo XML. Se comparte con usted para su referencia. El contenido específico es el siguiente
prueba de paquete; import java.io.fileInputStream; import java.io.filenotfoundexception; import java.io.fileOutputStream; import java.io.io.ioexception; import java.io.outputstream; import java.util.iterator; import javax.xml.namespace.namespaceText; import javax.xml.namespace.qname; import javax.xml.stream.xmlinputFactory; import javax.xml.stream.xmloutputFactory; import javax.xml.sstream.xmlstreamException; import javax.xml.sstream.xmlLeamReader; import javax.xml.xsteam; org.apache.axiom.om.OMAbstractFactory;import org.apache.axiom.om.OMAttribute;import org.apache.axiom.om.OMComment;import org.apache.axiom.om.OMContainer;import org.apache.axiom.om.OMDataSource;import org.apache.axiom.om.OMDocType;import org.apache.axiom.om.omDocument; import org.apache.axiom.om.omelement; import org.apache.axiom.om.omexception; importar org.apache.axiom.om.omfactory; importar org.apache.axiom.om.omnamespace; import org.apache.om.omsprocessinginTruction; org.apache.axiom.om.omSourcedElement; import org.apache.axiom.om.omtext; import org.apache.axiom.om.omxmlparserwrapers; import org.apache.axiom.om.impl.builder.straxumbuilder; import org.xml.sax.helpers.helpers.xmlreaderAryory; public class AxiomTest {public static void main (string [] args) lanza FileNotFoundException, lanzable {// leer xml fileInputStream xmlfile = new FileInputStream ("Line-Item2.xml"); XmlStreamReader parser = xmlinputFactory.newinStance (). CreateExmlStreamReader (xmlfile); // El objeto Staxombuilder también se requiere Staxombuilder Builder = new Staxombuilder (analizador); Omelement doc = builder.getDocumentElement (); // leer <foT> </foT> omelement cre = doc.getFirstChildWithName (nuevo qname ("estudiante")); // Leer <estudiante> omeLement Cre1 = Cre.getFirstChildWithName (nuevo qname ("id")); // Leer <Di> </id> System.out.println (Cre1.getLocalName ()+":"+Cre1.gettext ()); Cre1 = Cre.getFirstChildWithName (nuevo QName ("Nombre")); // Leer <Name> </name> System.out.println (Cre1.getLocalName ()+":"+Cre1.gettext ()); Cre1 = Cre.getFirstChildWithName (nuevo QName ("edad")); // Leer <Age> </Age> System.out.println (Cre1.getLocalName ()+":"+Cre1.gettext ()); Cre1 = Cre.getFirstChildWithName (nuevo QName ("Sex")); // Leer <Exe> </sexo> System.out.println (Cre1.getLocalName ()+":"+Cre1.gettext ()); Cre1 = Cre.getFirstChildWithName (nuevo QName ("Mensaje")); // Leer <Exe> </sexo> System.out.println (Cre1.getLocalName ()+":"+Cre1.gettext ()); Cre1 = Cre.getFirstChildWithName (nuevo QName ("Mensaje")); // Leer <Exe> </sexo> 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 (); A continuación, omDocument DOD = factory.createomDocument (); stu.addchild (factory.createomtext ("mac"); Conecte el nodo raíz al nodo DOC.AddChild (root); FileInputStream ("2.xml"); doc1.getChildelements (); System.out.println ("==================================================================================================================== = iter1.next (); OMF.CreateMelement ("Nombre", ","); Omelement Sex = OD.AddChild (root1);<? xml versión = "1.0" encoding = "utf-8"?> <fool> <student> <name> mac </name> <d id> 12 </id> <ge> 33 </dge> <sexo> masculino </ex> <message> hello world </sessage> </student> <druent> <nudent> silly </name> <did> 5 </id> <ge> 12 <//age </sexo </sexo </sexo </sexo </sexo </student> silly> silly </id> 5 </d id> <ge> 12 <//age </sexo </sexo </sexo </sexo </sexo </student> silly> silly </ideal> <Name> MR. Jones </same> <did> 2 </id> <ge> 31 </dge> <sexo> hombre </ sexo </sorector> <st alentador> <name> macy </name> <did> 2 </d id> <ge> 40 </ago> <sexo> femenino </ sexo> </sentudent> <derehor> <nere> tom </dame> <did> 32 </d id> <ge> 31 </cee> Mundo </sessage> </foT>
Otro ejemplo: leer archivos XML con Java
Los pasos para analizar XML son los siguientes:
Paquetes utilizados:
Objetos utilizados:
Métodos utilizados:
Vamos a analizar un archivo XML
import javax.xml.parsers.*; importar org.w3c.dom.*; importar org.xml.sax.*; Prueba de clase pública {public static void main (string [] args) {documentBuilderFactory dbf = documentBuilderFactory.newinstance (); intente {documentBuilder db = dbf.newDocumentBuilder (); Documento doc = db.parse ("pet2.xml"); Nodelist dogList = doc.getElementsBytagName ("perro"); System.out.println ("total" + dogList.getLength () + "nodos de perro"); para (int i = 0; i <dogList.getLength (); i ++) {node dog = dogList.Item (i); Elemento elem = (elemento) perro; System.out.println ("id:" + elem.getAttribute ("id")); for (nodo nodo = dog.getFirstChild (); node! = null; node = node.getNextSibling ()) {if (node.getNodeType () == node.element_node) {string name = node.getNodeName (); String value = node.getFirstChild (). GetNodeValue (); System.out.print (nombre + ":" + valor + "/t"); }} System.out.println (); }} catch (Exception e) {E.PrintStackTrace (); }}}Archivos 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"> <name> Qq </name> <alth> 100 </alth> <ve> 20 </ave> <sexo> qzi </ sexo> </penguin> </ PETS> </ PETS>
Lo anterior se trata de este artículo, espero que sea útil para el aprendizaje de todos.