zoukankan      html  css  js  c++  java
  • Java中Dom4j解析XML

    与利用DOM、SAX、JAXP机制来解析xml相比DOM4J表现更优秀,具有性能优异、功能强大和极端易用使用的特点,只要懂得DOM基本概念,就可以通过dom4j的api文档来解析xml.dom4j是一套开源的api。实际项目中,往往选择dom4j来作为解析xml的利器。

    sax解析: http://www.cnblogs.com/gavinYang/p/3505543.html
    jdom解析: http://www.cnblogs.com/gavinYang/p/3505530.html  
    dom解析: http://www.cnblogs.com/gavinYang/p/3505523.html

     1 package com.test;
     2 
     3 import java.io.File;
     4 import java.util.ArrayList;
     5 import java.util.Iterator;
     6 import java.util.List;
     7 
     8 import org.dom4j.Document;
     9 import org.dom4j.Element;
    10 import org.dom4j.io.SAXReader;
    11 
    12 public class Dom4jXML {
    13     
    14     public static void main(String[] args) {
    15         SAXReader saxReader = new SAXReader();
    16         File file = new File("e:/People.xml");
    17         
    18         try {
    19             Document document = saxReader.read(file);
    20             Element root = document.getRootElement();
    21             List<People> peoples = new ArrayList<People>(); 
    22             for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {
    23                 People people = new People();
    24                 Element peopleElement = (Element) iterator.next();
    25                 people.setId(peopleElement.attribute("id").getValue());
    26                 for (Iterator childIterator = peopleElement.elementIterator(); childIterator.hasNext();) {
    27                     Element childPeopleElement = (Element) childIterator.next();
    28                     if (childPeopleElement.getName().equals("Name")) {
    29                         people.setEnglishName(childPeopleElement.attribute("en").getValue());
    30                         people.setName(childPeopleElement.getText());
    31                     } else if (childPeopleElement.getName().equals("Age")) {
    32                         people.setAge(childPeopleElement.getText());
    33                     }
    34                 }
    35                 peoples.add(people);
    36             }
    37             
    38             for (People p : peoples) {  
    39                 System.out.println(p.getId()+"	"+p.getName()+"	"+p.getEnglishName()+"	"+p.getAge());  
    40             }  
    41 
    42         } catch (Exception e) {
    43             // TODO Auto-generated catch block
    44             e.printStackTrace();
    45         }
    46 
    47     }
    48 }

    People对象

     1 package com.test;
     2 
     3 public class People {
     4     private String id;
     5     private String name;
     6     private String englishName;
     7     private String age;
     8     public String getId() {
     9         return id;
    10     }
    11     public void setId(String id) {
    12         this.id = id;
    13     }
    14     public String getName() {
    15         return name;
    16     }
    17     public void setName(String name) {
    18         this.name = name;
    19     }
    20     public String getEnglishName() {
    21         return englishName;
    22     }
    23     public void setEnglishName(String englishName) {
    24         this.englishName = englishName;
    25     }
    26     public String getAge() {
    27         return age;
    28     }
    29     public void setAge(String age) {
    30         this.age = age;
    31     }
    32     
    33 }

    xml:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <PeopleList>
     3     <People id="1">
     4         <Name en='zhangsan'>张三</Name>
     5         <Age>20</Age>
     6     </People>
     7     <People id="2">
     8         <Name en='lisi'>李四</Name>
     9         <Age>30</Age>
    10     </People>
    11 </PeopleList>
  • 相关阅读:
    SharePoint Framework (SPFx) 开发入门教程
    SharePoint 2013 Designer 入门教程
    SharePoint 2013 开发教程
    SharePoint 2013 入门教程
    SharePoint Online 部署SPFx Web部件
    SharePoint Online SPFx Web部件绑定数据
    SharePoint Online 创建SPFx客户端Web部件
    SharePoint Online 配置框架(SPFx)开发环境
    SharePoint Online 创建应用程序目录
    SharePoint Online 启用 IRM
  • 原文地址:https://www.cnblogs.com/gavinYang/p/3505535.html
Copyright © 2011-2022 走看看