Dom4j right to use any xml parsing

java using dom4j for any xml parsing JAVA 2009-12-26 17:01:35 Read 23 Comments 2 Adjust Font Size:
Recently completed a java object generated for arbitrary xml, there are written for any one under the java parse xml file generated mainly by reflection principle to complete, while the resolution was mainly traverse xml tree each node, and each node treatment. Just the beginning, I wrote a resolution the following documents.

package javaForXML;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

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

/ **
* To use dom4j to parse XML
* Useless recursion, this class can only be resolved into two nested attributes and values
* @ Date Dec 25, 20094:43:47 PM
* @ Author zhangjp
* @ Param
* /
public class PraseXMLTest5 (

/ **
* Traverse each node and attribute
* @ Param filename
* @ ReturnPraseXMLTest5.java
* /
public static List <Map <String,String>> iterateWholeXML (String filename) (

List <Map <String,String>> list = new ArrayList <Map <String,String>> ();
SAXReader saxReader = new SAXReader ();

try (
Document document = saxReader.read (new File (filename));
Element root = document.getRootElement ();

/ / Traverse the root node of all the children nodes
for (Iterator iter = root.elementIterator (); iter.hasNext ();) (
HashMap <String, String> map = new HashMap <String, String> ();

Element element = (Element) iter.next ();
if (element == null)
continue;
/ / Get attribute and its value
for (Iterator attrs = element.attributeIterator (); attrs.hasNext ();) (
Attribute attr = (Attribute) attrs.next ();
if (attr == null)
continue;
String attrName = attr.getName ();
String attrValue = attr.getValue ();

map.put (attrName, attrValue);
)
if (element.isTextOnly ()) (
String elementName = element.getName ();
String elementValue = element.getText ();
map.put (elementName, elementValue);
) else (
/ / Traverse all children nodes nodes, and processing
for (Iterator iterInner = element.elementIterator (); iterInner.hasNext ();) (
Element elementInner = (Element) iterInner.next ();
/ / If there is no child nodes, then the direct values
if (elementInner == null) (
String elementName = element.getName ();
String elementValue = element.getText ();
map.put (elementName, elementValue);
)

/ / Child node attributes
for (Iterator innerAttrs = elementInner.attributeIterator (); innerAttrs.hasNext ();){
Attribute innerAttr = (Attribute) innerAttrs.next ();
if (innerAttr == null)
continue;
String innerAttrName = innerAttr.getName ();
String innerAttrValue = innerAttr.getValue ();
map.put (innerAttrName, innerAttrValue);
)
/ / Assume that no third layer of nesting to obtain the value of the second layer
String innerName = elementInner.getName ();
String innerValue = elementInner.getText ();
map.put (innerName, innerValue);

)

)
list.add (map);
)
return list;
) Catch (DocumentException e) (
/ / TODO Auto-generated catch block
e.printStackTrace ();
)
return null;

)

/ **
* Test
* @ Param args
* @ Throws DocumentExceptionPraseXMLTest5.java
* /
public static void main (String [] args) throws DocumentException (
String filename = "d: \ \ data.xml";
List <Map <String,String>> list = PraseXMLTest5.iterateWholeXML (filename);
for (Map <String,String> map: list) (
for (String ss: map.keySet ()) (
System.out.println (ss + ":" + map.get (ss));
)
)
)
)

Very tired of this writing, specifically said it was very cumbersome, and only two-level traversal xml nesting pairs of three or more nested impotence inability. Thus it is clear to see here how easy it is to use a recursive-type things. So wrote the following code:

package javaForXML;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

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

/ **
* To use dom4j to parse XML
* Use of recursion, this type nested recursive traversal of all
* @ Date Dec 25, 20094:43:47 PM
* @ Author zhangjp
* @ Param
* /
public class PraseXMLTest6 (

/ **
* Traverse each node and attribute
* @ Param filename
* @ ReturnPraseXMLTest5.java
* /
public static List <Map <String,String>> iterateWholeXML (String filename) (

List <Map <String,String>> list = new ArrayList <Map <String,String>> ();
SAXReader saxReader = new SAXReader ();

try (
Document document = saxReader.read (new File (filename));
Element root = document.getRootElement ();
recursiveNode (root, list);
return list;
) Catch (DocumentException e) (
/ / TODO Auto-generated catch block
e.printStackTrace ();
)
return null;

)

/ **
* Recursively traverse all the nodes to obtain the corresponding value of
* @ Param rootPraseXMLTest6.java
* /
private static void recursiveNode (Element root, List <Map <String,String>> list) (
/ / Traverse the root node of all the children nodes
for (Iterator iter = root.elementIterator (); iter.hasNext ();) (
HashMap <String, String> map = new HashMap <String, String> ();

Element element = (Element) iter.next ();
if (element == null)
continue;
/ / Get attribute and its value
for (Iterator attrs = element.attributeIterator (); attrs.hasNext ();) (
Attribute attr = (Attribute) attrs.next ();
if (attr == null)
continue;
String attrName = attr.getName ();
String attrValue = attr.getValue ();

map.put (attrName, attrValue);
)
/ / If there is PCDATA, then the direct
if (element.isTextOnly ()) (
String innerName = element.getName ();
String innerValue = element.getText ();
map.put (innerName, innerValue);
list.add (map);
) else (
list.add (map);
/ / Recursive call
recursiveNode (element, list);
)

)
)

/ **
* Test
* @ Param args
* @ Throws DocumentExceptionPraseXMLTest5.java
* /
public static void main (String [] args) throws DocumentException (
String filename = "d: \ \ zhang.xml";
List <Map <String,String>> list = PraseXMLTest6.iterateWholeXML (filename);
for (Map <String,String> map: list) (
for (String ss: map.keySet ()) (
System.out.println (ss + ":" + map.get (ss));
)
)
)
)

After using a recursive, save a lot of code. Seem relatively straightforward, the biggest advantage is that for any number of nested xml file parsing are alone, HA HA ~ Okay, this analysis should be simpler than that generated more than a good Wu-bar.

分类:Java 时间:2010-03-18 人气:239
分享到:
blog comments powered by Disqus

相关文章

  • xml and java Bean's direct conversion, Xmap 2010-08-22

    Recent projects use the xml and java bean conversion used when we need to traverse the xml parsing. . . Then resolved into a statement the value of a good java bean, a very painful process, the result is very good that we can not be directly converte

  • java xml parsing and import database (dom4j) 2010-07-11

    java xml parsing and import database (dom4j) import java.io.File; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.

  • Using java reflection principle to write a simple class assignment and common values 2011-08-22

    Since the actual needs, so the use of java reflection principle to write a simple assignment and values ​​common to the bean class in this record is used to facilitate his own future, but also need to provide a reference example of the brothers. Of c

  • XML parsing in Java, 2010-04-11

    XML in the process of developing more and more important, we often need to parse XML documents in the programming. Parsing XML documents in Java in a variety of ways, each approach has its own characteristics. Here we use a different analytical metho

  • Java, four types of XML parsing technologies 2010-04-26

    In ordinary work, inevitably face the XML as a data storage format. Faced with a wide range of solutions, which best for us? In this article, I am one of four major programs do not complete evaluation, only traverse the XML for the piece to test, bec

  • java code Note 2010-06-03: StringTokenizer and the use of HashMap instance 2010-06-07

    -------- StringTokenizer and the use of HashMap instance ------------ import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.StringTokenizer; public class jay_x_stringtokenizer_map ( void sta

  • JAVA Fourth manners xml parsing 2010-08-07

    XML file format is as follows: <? Xml version = "1.0" encoding = "GB2312"?> <RESULT> <VALUE> <NO> 123123131 </ NO> <ADDR> XX XX County, XX Town, Province Road X XX XX Section No. </ ADDR> </

  • On Java, four kinds of XML parsing technologies are not fully tested 2010-09-05

    Routine work, inevitably face the XML as a data storage format. Faced with a wide range of solutions, which best for us? In this article, I am one of four major programs do not complete evaluation, only traverse the XML for the piece to test, because

  • Java, XML parsing techniques of four 2010-10-28

    In routine work, will inevitably encounter the XML as a data storage format. Faced with a wide variety of solutions, which best for us? In this article, I am one of four major programs do not fully evaluation, just to test this for traversing XML, be

  • java create xml parsing 2010-11-10

    These days is always someone asked how to use java generate xml file, before also see how others used their own, under their own will probably understand, but we will not be the specific wording, so look for the next under the relevant documentation

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.738 (s). 12 q(s)