Java DOM4J Parser - Analisar documento XML

Passos para usar DOM4J

A seguir estão as etapas usadas ao analisar um documento usando o analisador DOM4J.

  • Importe pacotes relacionados a XML.

  • Crie um SAXReader.

  • Crie um documento a partir de um arquivo ou fluxo.

  • Obtenha os nós necessários usando XPath Expression chamando document.selectNodes ()

  • Extraia o elemento raiz.

  • Repita a lista de nós.

  • Examine os atributos.

  • Examine os subelementos.

Importar pacotes relacionados a XML

import java.io.*;
import java.util.*;
import org.dom4j.*;

Crie um DocumentBuilder

SAXBuilder saxBuilder = new SAXBuilder();

Crie um documento a partir de um arquivo ou fluxo

File inputFile = new File("input.txt");
SAXBuilder saxBuilder = new SAXBuilder();
Document document = saxBuilder.build(inputFile);

Extraia o elemento raiz

Element classElement = document.getRootElement();

Examine os atributos

//returns specific attribute
valueOf("@attributeName");

Examine os subelementos

//returns first child node
selectSingleNode("subelementName");

Exemplo de demonstração

Aqui está o arquivo xml de entrada que precisamos analisar -

<?xml version = "1.0"?>
<class>
   <student rollno = "393">
      <firstname>dinkar</firstname>
      <lastname>kad</lastname>
      <nickname>dinkar</nickname>
      <marks>85</marks>
   </student>
   
   <student rollno = "493">
      <firstname>Vaneet</firstname>
      <lastname>Gupta</lastname>
      <nickname>vinni</nickname>
      <marks>95</marks>
   </student>
   
   <student rollno = "593">
      <firstname>jasvir</firstname>
      <lastname>singn</lastname>
      <nickname>jazz</nickname>
      <marks>90</marks>
   </student>
</class>

DOM4JParserDemo.java

package com.tutorialspoint.xml;

import java.io.File;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class DOM4JParserDemo {

   public static void main(String[] args) {

      try {
         File inputFile = new File("input.txt");
         SAXReader reader = new SAXReader();
         Document document = reader.read( inputFile );

         System.out.println("Root element :" + document.getRootElement().getName());

         Element classElement = document.getRootElement();

         List<Node> nodes = document.selectNodes("/class/student" );
         System.out.println("----------------------------");
         
         for (Node node : nodes) {
            System.out.println("\nCurrent Element :"
               + node.getName());
            System.out.println("Student roll no : " 
               + node.valueOf("@rollno") );
            System.out.println("First Name : "
               + node.selectSingleNode("firstname").getText());
            System.out.println("Last Name : "
               + node.selectSingleNode("lastname").getText());
            System.out.println("First Name : "
               + node.selectSingleNode("nickname").getText());
            System.out.println("Marks : "
               + node.selectSingleNode("marks").getText());
         }
      } catch (DocumentException e) {
         e.printStackTrace();
      }
   }
}

Isso produziria o seguinte resultado -

Root element :class
----------------------------

Current Element :student
Student roll no : 
First Name : dinkar
Last Name : kad
First Name : dinkar
Marks : 85

Current Element :student
Student roll no : 
First Name : Vaneet
Last Name : Gupta
First Name : vinni
Marks : 95

Current Element :student
Student roll no : 
First Name : jasvir
Last Name : singn
First Name : jazz
Marks : 90