Este artigo compartilha o método dos arquivos XML de análise/leitura de Java para sua referência. O conteúdo específico é o seguinte
Arquivos XML
<? xml versão = "1.0"?> <dudits> <estudante> <name> john </name> <rade> b </ruda> <eane> 12 </a Age> </student> <denty> <name> Mary </name> </grau </grau> <routh> <eane> 11 </idade
Código Java:
pacote net.viralpatel.java.xmlparser; importar java.io.file; importar javax.xml.parsers.documentBuilder; importar javax.xml.parsers.documentBuilderFactory; importar org.w3c.dom.document; importar org.w3c.dom.Element; importar org.w3c.dom.node; importar org.w3c.dom.nodelist; public class XmlParser {public void getAllerNames (string fileName) {try {documentBuilderFactory dbf = documentBuilderFactory.NewInstance (); Documentbuilder db = dbf.newdocumentBuilder (); Arquivo de arquivo = novo arquivo (nome do arquivo); if (file.exists ()) {document doc = db.parse (arquivo); Elemento docele = doc.getDocumentElement (); // Imprime o elemento raiz do documento System.out.println ("Elemento raiz do documento:" + docele.getNodename ()); Nodelist StudentList = Docele.GetElementsByTagName ("Student"); // Imprima o total de elementos do aluno no documento System.out .println ("Total de alunos:" + StudentList.getLength ()); if (StudentList! = null && StudentList.getLength ()> 0) {for (int i = 0; i <studentList.getLength (); i ++) {node = studentList.item (i); if (node.getNodEType () == Node.Element_Node) {System.out .println ("====================="); Nó do elemento e = (elemento); Nodelist nodelist = e.getElementsByTagName ("nome"); System.out.println ("Nome:" + nodelist.item (0) .getChildNodes (). Item (0) .getNodEvalue ()); nodelist = e.getElementsByTagName ("grau"); System.out.println ("Grade:" + NodElist.item (0) .getChildNodes (). Item (0) .getNodEvalue ()); nodelist = e.getElementsByTagName ("idade"); System.out.println ("Age:" + nodelist.item (0) .getChildNodes (). Item (0) .getNodEvalue ()); }} else {System.exit (1); }}} catch (Exceção e) {System.out.println (e); }} public static void main (string [] args) {xmlparser parser = new xmlParser (); parser.getAllUserNames ("c: //test.xml"); }}O exposto acima é tudo sobre este artigo, espero que seja útil para o aprendizado de todos.