dom4j parsing


1: Common methods of building dom4j tree (represented as Document element) :
Method 1: Create all elements directly: dom4j has prepared a tool class DocumentHelper for us, all the methods of this class are static methods, which are used to create the various components of the xml document part.

1.1.1 Create Document and Element objects:
Document doc=DocumentHelper.createDocument();
Element eltRoot=DocumentHelper.createElement(“student”);
doc.setEltRootElement(eltRoot);

1.1.2 Or prepare the root element first, use parameters The constructor creates a Document object.
Element eltRoot=DocumentHelper.createElement(“student”);
Document doc=DocumentHelper.createDocument(eltRoot);

1.2: Add node and set node content:
method: method defined in Branch interface;
public Element addElemen(String name) //with The specified name creates a child node for the current node and returns the reference of the new node
public void setText(String text) //Set content to the content of the node

Example as follows:
Element eltName=eltRoot.addElement(“name”);
Element eltAge=eltRoot.addElement("age");
eltName.setText("Zhang San");
eltAge.setText("18");

1.3: Add attribute

Method : public Element addAttribute(String name, String value) The


example is as follows:
eltRoot.addAttribute("sn","01");

Mode 2:
2.1: org.dom4j.io provides two classes: SAXReader and DOMReader, the former builds a dom4j tree from an existing w3c DOM tree, while SAXReader uses SAX parser that builds dom4j trees from different input sources.

2.1.1: Use SAXReader to build a dom4j document object: the example is as follows:

SAXReader saxReader=new SAXReader();
File file=new File(“student.xml”);
Document doc=saxReader.read(file);

2.2.1: Use DOMReader builds dom4j document objects.

DocumentBuilderFactory dbf=new DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
File file=new File(“student.xml”);
org.w3c.dom.Document document=db.parse(file);
DOMReader domReader=new DOMReader();
org.dom4j.Document doc=domReader.read(document) ;

Access root node:
Element root=doc.getRootElement();
Access node:
Access all nodes:
java.util.List childrenList=elt.elements();

Access all nodes with specified name
java.util.Liat childrenList=elt.elements ("student");

access the first node of the specified name
Element eltChild=elt.element("student");

To iterate all child elements of an element:
for(java.util.Iterator it=root.elementIterator(); it.hasNext()){
Element element =it.hasNext();
...
}

Dom4j has integrated XPath support. When selecting nodes, XPath expressions can be used directly, for example:
To select all the <name> elements in the example file students.xml, the code is as follows:
java.util.List l=root.selectNodes(“//name”);

To select the <student> element whose attribute sn is equal to 01 , the code is as follows:
java.util.List l=root.selectNodes("//student[@sn='01']");

Note: In order to compile and execute the above code using XPath expressions, it is necessary to configure the dom4j installation package Comes with jaxen package, you can also download jaxen from http://sourceforge.net/products/jaxen/. jaxen is an XPath engine developed in java for use with various XML-based object models such as DOM, dom4j and JDOM. In the dom4-1.6.1 directory, there is a lib subdirectory, which contains a jaxen-1.1-beta-6.jar file, and the full path name of the file needs to be configured in the classpath environment variable.

Access attributes:
To get all attributes of an element, as follows:
java.util.List attrList=elt.attributes();
To get the specified attributes, as follows:
Attribute attr=elt.attribute("sn");
To get The value of an attribute; as follows:
String attrValue=elt.attributeValue("sn");

Delete node and node attributes:
To delete an element:: You can use the remove() method defined in the Branch interface, as follows:
Element eltStu=root.element("student");
root.remove(eltStu);

To delete an attribute; as follows:
elt.remove(elt.attribute("sn"));

Output document:
Object: XMLWriter Instance method:
A summary of several commonly used construction methods for write (Document doc) :
1: No parameter:
2: Byte stream parameter
3: Character stream parameter.
4: Document output format class parameters.
Sample code 1:
Output document content to console
XMLWriter xw=new XMLWriter();
xw.write(doc);
Sample code 2:
Output document content to file. When constructing an XMLWriter object, you can pass byte stream parameters. Its underlying code sets up an automatic refresh mechanism.
XMLWriter xw=new XMLWriter(new java.io.FileOutputStream(“student.xml”));
xw.write(doc);

Example code 3:
When constructing an XMLWriter object, you can pass a java.io.Writer object.
XMLWriter xw=new XMLWriter(new java.io.FileWriter("student.xml"));                                                                        
xw.write(doc);
xw.close();
Note: XMLWriter objects constructed with java.io.Writer objects do not have Set the automatic refresh mechanism, so after calling the write() method, call the xw.close() or xw.flush() method. And XMLWriter inherits from the org.xml.sax.helpers.XMLFilterImpl class. The close() and flush() methods it provides are just wrappers for the flush() and close() methods of its internal java.io.Writer object.

Example code 4:
When constructing the XMLWriter object, you can pass the document output format class org.dom4j.io.OutputFormat. Using this class, you can set the character encoding of the output document, set the line separator, and control the indentation string used.

The output format of the code below uses 4 spaces as an indent string with new lines added between elements.

OutputFormat outFmt=new OutputFormat(“ ”,true);
XMLWriter xw=new XMLWriter(outFmt);
xw.write(doc);

The following code uses a beautified format to output the document, sets the string encoding to GB2312, and uses 4 spaces as indentation.
OutputFormat outFmt=OutputFormat.createPrettyPrint();
outFmt.setEncoding("GB2312");
outFmt.setIndent(" ");
XMLWriter xw=new XMLWriter(outFmt);
xw.write(doc); Commonly used for

dom4j namespace information api
There are 8 methods.

dom4j defines methods for obtaining namespace information in the Element and Attribute interfaces, which are the same as in JDOM. As follows:
public java.lang.String getNamespacePrefix()
This method returns the namespace prefix of the element (attribute)
public java.lang.String getNamespaceURI()
This method returns the namespace URI of the element (attribute)
public java.lang.String getName()
This method returns the local name of the element (attribute)
public java.lang.String getQualifiedName()
This method returns the qualified name of the element (attribute)
public Namespace getNamespace()
This method returns the namespace of the element itself
public java.util. List additionalNamespaces()
Returns a list of namespace declarations attached to an element, each of which is of type Namespace. The methods of this class provide two methods to obtain namespace prefix and local name respectively. As follows:

public java.lang.String getPrefix()
This method returns the namespace prefix.
public java.lang.String getURI()
This method returns the URI of the namespace.


-------------------------------------------------- -

DOM4J parsing technology
1. Document object related

(1) Read XML file, get document object

eg ->

SAXReader reader = new SAXReader();
Document document = reader.read(new File(emp.xml"));

  


(2 ) Parse the text in XML to get the document object

eg ->

String text = "element";
Document document = DocumentHelper.parseText(text);

  

(3) Actively create the document object

eg ->

Document document = DocumentHelper.createDocument();
Element root = document.addElement("members");// Create the root node

  
2. Node related

(1) Get the root node of the document.

eg ->

SAXReader reader = new SAXReader();
Document document = reader.read(new File (emp.xml"));

  

(2) Get a single child node of a node

eg ->

Element memberElm=root.element("member");// "member" is the node name

  

(3) Get the text of the node

eg - >

DString text=memberElm.getText();

Or:

eg ->

String text=root.elementText("name");//This is the text to get the name byte under the root node

  

(4) Get the name under a node For all the byte points of "member" and traverse

eg ->

List nodes = rootElm.elements("member");
    for (Iterator it = nodes.iterator(); it.hasNext(); ) {
        Element elm = (Element) it.next(); // do something
    }

  

(5) Traverse all child nodes under a node

eg ->

for(Iterator it=root.elementIterator();it.hasNext();){
    Element element = (Element) it.next(); // do something
}

  

(6) Add a child node under a node

eg ->

Element ageElm = newMemberElm.addElement("age");

  

(7) Set the node text.

eg ->

ageElm.setText("29");

  

(8) Delete a node

eg ->

parentElm.remove(childElm);// childElm is the node to be deleted, parentElm is its parent node

  

(9) Add a CDATA node

eg ->

Element contentElm = infoElm.addElement("content");
contentElm .addCDATA(diary.getContent());
contentElm.getText(); //Special note: Get the CDATA value of the node and get the value of the node is a method
contentElm.clearContent(); //Clear the content in the node, CDATA also Can

  
3. Attribute related

(1) Get an attribute under a node

eg ->

Element root=document.getRootElement();
Attribute attribute=root.attribute("size");// Attribute name name

  

(2) Get the text of the attribute

eg ->

String text=attribute.getText();

  

or: eg ->

String text2=root.element("name").attributeValue("firstname"); //This is to get the attribute of the name byte point under the root node The value of firstname.

  

(3) Traverse all attributes of a node

eg ->

Element root=document.getRootElement();
for(Iterator it=root.attributeIterator();it.hasNext();){
    Attribute attribute = (Attribute) it.next();
    String text=attribute.getText();
    System.out.println(text);
}

  

(4) Set the attributes and text of a node

eg ->

newMemberElm.addAttribute("name", "sitinspring");

  

(5) Set the text of the attribute

eg ->

Attribute attribute=root.attribute("name");
attribute.setText("sitinspring");

  

(6) Delete an attribute

eg ->

Attribute attribute=root.attribute("size");// attribute name name
root.remove(attribute);

  
4. Write the document to the XML file

(1) The document is all in English, no encoding is set, just write Enter the form

eg ->

XMLWriter writer = new XMLWriter(new FileWriter("output.xml"));
writer.write(document);
writer.close();

  

(2) The document contains Chinese, set the encoding format to write Form

eg ->

OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GBK"); // Specify XML encoding
XMLWriter writer = new XMLWriter(new FileWriter("output.xml"),format);
writer.write(document);
writer.close();

  
5. Conversion of strings and XML

(1) Convert strings to XML

eg ->

String text = " sitinspring ";
Document document = DocumentHelper.parseText(text);

  

(2) Convert the XML of the document or node to a string

eg ->

SAXReader reader = new SAXReader();
Document document = reader.read( new File("input.xml"));
Element root=document.getRootElement();
String docXmlText=document.asXML();
String rootXmlText=root.asXML();
Element memberElm=root.element("member");
String memberXmlText=memberElm.asXML();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326149147&siteId=291194637