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.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Dom4j right to use any xml parsing

  • In the servlet use Bean

    According to Sun's definition, JavaBean is a reusable software components. In fact JavaBean is a Java class, through the package into a property and methods of treatment of a function or a business object, referred to as bean. Because JavaBean is ...

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Servlet brief introduction

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries