Java读取XML 之 Java DOM解析器示例

在此Java xml解析器教程中,学习使用 Java中的DOM解析器读取xml。DOM解析器旨在将XML作为内存中的对象图(树状结构)使用-称为“ 文档对象模型(DOM) ”。

首先,解析器遍历输入的XML文件并创建与XML文件中的节点相对应的DOM对象。这些DOM对象以树状结构链接在一起。一旦解析器完成了解析过程,我们就会从中获得这种树状DOM对象结构。现在,我们可以根据需要来回遍历DOM结构-从中获取/更新/删除数据。

目录

1. DOM解析器API- 
   导入与XML相关的包- 
   创建
   DocumentBuilder-从文件或 Stream 
   创建文档
   - 
   验证文档结构-提取根元素-检查属性
   -检查子元素
2.使用DOM解析器读取XML 
3。将数据读取到POJO对象
4。使用DOM解析器解析“未知” xml

阅读更多:DOM解析器和SAX解析器之间的区别

出于示例目的,我们将在所有代码示例中解析xml内容。

员工.xml
<employees>
    <employee id="111">
        <firstName>Lokesh</firstName>
        <lastName>Gupta</lastName>
        <location>India</location>
    </employee>
    <employee id="222">
        <firstName>Alex</firstName>
        <lastName>Gussin</lastName>
        <location>Russia</location>
    </employee>
    <employee id="333">
        <firstName>David</firstName>
        <lastName>Feezor</lastName>
        <location>USA</location>
    </employee>
</employees>

1. DOM解析器API

让我们记下一些创建和使用DOM解析器来解析Java中XML文件的主要步骤。

DOM解析器的实际应用
DOM解析器的实际应用

1.1。导入dom解析器包

我们将需要首先在我们的应用程序中导入dom解析器包。

import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;

1.2。创建DocumentBuilder

下一步是创建DocumentBuilder对象。

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

1.3。从xml文件创建Document对象

读取XML文件以使其成为Document对象。

Document document = builder.parse(new File( file ));

1.4。验证文件结构

XML验证是可选的,但最好在开始解析之前进行验证。

Schema schema = null;
try {
  String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
  SchemaFactory factory = SchemaFactory.newInstance(language);
  schema = factory.newSchema(new File(name));
} catch (Exception e) {
    e.printStackStrace();
}
Validator validator = schema.newValidator();
validator.validate(new DOMSource(document));

1.5。提取根元素

我们可以使用以下代码从XML文档中获取根元素。

Element root = document.getDocumentElement();

1.6。检查属性

我们可以使用以下方法检查xml元素属性。

element.getAttribute("attributeName") ;    //returns specific attribute
element.getAttributes();                //returns a Map (table) of names/values

1.7。检查子元素

子元素可以以下方式查询。

node.getElementsByTagName("subElementName") //returns a list of sub-elements of specified name
node.getChildNodes()                         //returns a list of all child nodes

2.使用DOM解析器读取XML

在下面的示例代码中,我假设用户已经知道employees.xml文件的结构(它的节点和属性);因此,示例直接开始获取信息并开始在控制台中打印信息。在现实生活中的应用程序中,我们将这些信息用于某些实际目的,而不是在控制台上打印并离开。

//Get Document Builder
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
//Build Document
Document document = builder.parse(new File("employees.xml"));
//Normalize the XML Structure; It's just too important !!
document.getDocumentElement().normalize();
//Here comes the root node
Element root = document.getDocumentElement();
System.out.println(root.getNodeName());
//Get all employees
NodeList nList = document.getElementsByTagName("employee");
System.out.println("============================");
for (int temp = 0; temp < nList.getLength(); temp++)
{
 Node node = nList.item(temp);
 System.out.println("");    //Just a separator
 if (node.getNodeType() == Node.ELEMENT_NODE)
 {
    //Print each employee's detail
    Element eElement = (Element) node;
    System.out.println("Employee id : "    + eElement.getAttribute("id"));
    System.out.println("First Name : "  + eElement.getElementsByTagName("firstName").item(0).getTextContent());
    System.out.println("Last Name : "   + eElement.getElementsByTagName("lastName").item(0).getTextContent());
    System.out.println("Location : "    + eElement.getElementsByTagName("location").item(0).getTextContent());
 }
}

程序输出:

安慰
employees
============================
Employee id : 111
First Name : Lokesh
Last Name : Gupta
Location : India
Employee id : 222
First Name : Alex
Last Name : Gussin
Location : Russia
Employee id : 333
First Name : David
Last Name : Feezor
Location : USA

3.将数据读取到POJO对象

另一个现实生活中的应用程序的要求可能是用在上面的示例代码中获取的信息填充DTO对象。我写了一个简单的程序来帮助您了解如何轻松完成它。

假设我们必须填充Employee定义如下的对象。

public class Employee
{
   private Integer id;
   private String firstName;
   private String lastName;
   private String location;
   
   //Setters and Getters
   
   @Override
   public String toString()
   {
      return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", location=" + location + "]";
   }
}

现在查看示例代码以填充员工对象列表。就像在代码之间插入几行,然后将值复制到DTO而不是控制台中一样简单。

Java程序,用于使用DOM解析器读取XML文件。

PopulateDTOExamplesWithParsedXML.java
public class PopulateDTOExamplesWithParsedXML
{
   public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException
   {
        List<Employee> employees = parseEmployeesXML();
        System.out.println(employees);
   }
   private static List<Employee> parseEmployeesXML() throws ParserConfigurationException, SAXException, IOException
   {
      //Initialize a list of employees
      List<Employee> employees = new ArrayList<Employee>();
      Employee employee = null;
      
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();
      Document document = builder.parse(new File("employees.xml"));
      document.getDocumentElement().normalize();
      NodeList nList = document.getElementsByTagName("employee");
      for (int temp = 0; temp < nList.getLength(); temp++)
      {
         Node node = nList.item(temp);
         if (node.getNodeType() == Node.ELEMENT_NODE)
         {
            Element eElement = (Element) node;
            //Create new Employee Object
            employee = new Employee();
            employee.setId(Integer.parseInt(eElement.getAttribute("id")));
            employee.setFirstName(eElement.getElementsByTagName("firstName").item(0).getTextContent());
            employee.setLastName(eElement.getElementsByTagName("lastName").item(0).getTextContent());
            employee.setLocation(eElement.getElementsByTagName("location").item(0).getTextContent());
            
            //Add Employee to list
            employees.add(employee);
         }
      }
      return employees;
   }
}

程序输出。

安慰
[Employee [id=111, firstName=Lokesh, lastName=Gupta, location=India],
Employee [id=222, firstName=Alex, lastName=Gussin, location=Russia],
Employee [id=333, firstName=David, lastName=Feezor, location=USA]]

4.使用DOM解析器解析“未知” xml

上一个示例显示了在编写代码时,我们如何遍历具有已知或几乎不知道的结构的XML文档。在某些情况下,我们可能必须以某种方式编写代码,以便即使在编码时假定的XML结构存在某些差异,程序也必须能够正常运行。

在这里,我们遍历XML文档树中存在的所有元素。我们可以添加我们的知识并修改代码,以便在遍历树时只要获得所需信息,我们就可以使用它。

ParseUnknownXMLStructure.java
public class ParseUnknownXMLStructure
{
   public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException
   {
      //Get Document Builder
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();
      
      //Build Document
      Document document = builder.parse(new File("employees.xml"));
      
      //Normalize the XML Structure; It's just too important !!
      document.getDocumentElement().normalize();
      
      //Here comes the root node
      Element root = document.getDocumentElement();
      System.out.println(root.getNodeName());
      
      //Get all employees
      NodeList nList = document.getElementsByTagName("employee");
      System.out.println("============================");
      
      visitChildNodes(nList);
   }
   //This function is called recursively
   private static void visitChildNodes(NodeList nList)
   {
      for (int temp = 0; temp < nList.getLength(); temp++)
      {
         Node node = nList.item(temp);
         if (node.getNodeType() == Node.ELEMENT_NODE)
         {
            System.out.println("Node Name = " + node.getNodeName() + "; Value = " + node.getTextContent());
            //Check all attributes
            if (node.hasAttributes()) {
               // get attributes names and values
               NamedNodeMap nodeMap = node.getAttributes();
               for (int i = 0; i < nodeMap.getLength(); i++)
               {
                   Node tempNode = nodeMap.item(i);
                   System.out.println("Attr name : " + tempNode.getNodeName()+ "; Value = " + tempNode.getNodeValue());
               }
               if (node.hasChildNodes()) {
                  //We got more childs; Let's visit them as well
                  visitChildNodes(node.getChildNodes());
               }
           }
         }
      }
   }
}

程序输出。

安慰
employees
============================
Node Name = employee; Value =
        Lokesh
        Gupta
        India
    
Attr name : id; Value = 111
Node Name = firstName; Value = Lokesh
Node Name = lastName; Value = Gupta
Node Name = location; Value = India
Node Name = employee; Value =
        Alex
        Gussin
        Russia
    
Attr name : id; Value = 222
Node Name = firstName; Value = Alex
Node Name = lastName; Value = Gussin
Node Name = location; Value = Russia
Node Name = employee; Value =
        David
        Feezor
        USA
    
Attr name : id; Value = 333
Node Name = firstName; Value = David
Node Name = lastName; Value = Feezor
Node Name = location; Value = USA

这就是有关Java XML DOM Parser的概念的全部内容。如果不清楚或需要更多说明,请给我评论。

学习愉快!

参考:

http://www.w3c.org/DOM/

This entry was posted in   Xml.
Bookmark the   permalink.

saigon has written 1440 articles

Leave a Reply