DOM4J parsing messages

Source: Internet
Author: User

Import Java.io.File;
Import Java.util.Iterator;
Import java.util.List;
Import Javax.xml.parsers.DocumentBuilder;
Import Javax.xml.parsers.DocumentBuilderFactory;
Import Org.dom4j.Attribute;
Import org.dom4j.Document;
Import org.dom4j.Element;
Import Org.dom4j.VisitorSupport;
Import Org.dom4j.io.DOMReader;

public class Dom4jtest {


public static void Main (string[] args) throws Exception {
Change to dom4j mode parsing
Domreader domreader=new Domreader ();
Building a Document object in the audience
Documentbuilderfactory factory=documentbuilderfactory.newinstance ();
Documentbuilder Db=factory.newdocumentbuilder ();
Getting document objects from the document builder
File File=new file ("Src/books.xml");
Org.w3c.dom.Document domdocument= db.parse (file);

Document Doc=domreader.read (domDocument);


Start Parsing Doc Object
Element root= doc.getrootelement ();

Get by ID
Element book1= Doc.elementbyid ("BH10001");
System.out.println (Book1.asxml ());

Gets the first child node of the current node
Element book= root.element ("book");
System.out.println (Book.asxml ());

List list= root.elements ();
for (Iterator it = List.iterator (); It.hasnext ();) {
element element = (Element) It.next ();
System.out.println (Element.asxml ());
}

List list2= root.elements ("book");
for (Iterator it = List2.iterator (); It.hasnext ();) {
element element = (Element) It.next ();
List list3=element.elements ("name");
System.out.println ((Element) list3.get (0)). Asxml ());

}
Element book2= (Element) root.elements (). get (1);
System.out.println (Book2.asxml ());
String Id=book2.attribute ("id"). getText ();
SYSTEM.OUT.PRINTLN (ID);
Querying XML nodes through the XPath path language
Element xbook2 = (Element) Root.selectsinglenode ("//book[1]");
System.out.println (Xbook2.asxml ());
Attribute xbook2id = (Attribute) root.selectsinglenode ("//book[1]/@id");
Element xbook2id = (Element) Root.selectsinglenode ("/books/book[2]/name");
System.out.println (Xbook2id.gettext ());

List listx=root.selectnodes ("/books/book");
for (Iterator it = Listx.iterator (); It.hasnext ();) {
element element = (Element) It.next ();
System.out.println (element.element ("name"). GetText ());
}
}

}

Message content:

<?xml version= "1.0" encoding= "Utf-8"?>
<books>
<book id= "BH10001" id= "BH10001" haha= "haha" >
<name>java Learning </name>
<price>100</price>
<author>itany</author>
</book>
<book id= "BH10002" >
<name>xml</name>
<price>200</price>
<author>itany</author>
</book>
<book id= "BH10003" >
<name>js</name>
<price>300</price>
<author>itany</author>
</book>
<book id= "BH10004" >
<name>jsp</name>
<price>400</price>
<author>itany</author>
</book>
<book id= "BH10005" >
<name>mysql</name>
<price>500</price>
<author>itany</author>
</book>
</books>

DOM4J parsing messages

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.