Java querying XML elements using dom4j

Source: Internet
Author: User

1.Java querying XML elements using dom4j:

The 1.1book.xml file is as follows:
<?xml version= "1.0" encoding= "UTF-8"?>

<books>
<book>
<id>a1</id>
<name> Crazy Java Handout (with CD) </name>
<author> Li Gang Authoring </author>
<price>74.20</price>
<image>java.jpg</image>
<remark>
<! [Cdata[Madness comes from dreams, technical achievements This book comes from the author's 3 years of Java training experience, condensed the author's nearly 3,000 hours of teaching experience,<br/> summed up hundreds of Java learners in the process of learning typical errors.] ></remark>
</book>
<book>
<id>a2</id>
<name> lightweight Java EE Enterprise Application combat </name>
<author> Li Gang Authoring </author>
<price>59.20</price>
<image>ee.jpg</image>
<remark> This book mainly introduces Spring+hibernate-based Java EE applications .</remark>
</book>
<book>

</books>

2. Querying XML Elements using dom4j: Create a Testpath class:

2.1 First to import the DOM4J package ++++++++++++++++++++++++++++++++++++++++++++++++++++

Import java.util.LinkedList; Import java.util.List;

Import org.dom4j.Document;

Import org.dom4j.DocumentException;

Import org.dom4j.Element;

Import Org.dom4j.io.SAXReader;

public class Testxpath {

public static void Main (string[] args) throws Documentexception {

Saxreader reader = new Saxreader ();

Document doc = Reader.read (TestXPath.class.getResourceAsStream ("/book.xml"));

Then add a method, based on the ID query.

The purpose of incoming DOC is to use the same document multiple times

list<book> list = getbooklist (DOC);

List.foreach (e) {System.out.println (e); });

System.out.println ("========================================");

Query using the same document again

Book B = Getbookbyid (Doc, "3");

System.out.println (b); }

private static book Getbookbyid (Document doc, String ID) {

Element e = (Element) Doc.selectsinglenode ("/books/book[id=" + ID + "']");

Book B = Tobook (e);

return b; }

private static book Tobook (Element e) {

Book B = new book ();

Gets the contents of the element named author in the child element

String author = e.elementtext ("author");

String id = e.elementtext ("id");

String image = E.elementtext ("image");

String name = E.elementtext ("name");

String Price = E.elementtext ("price");

String Remark = E.elementtext ("remark");

B.setauthor (author);

B.setid (ID);

B.setimage (image);

B.setname (name);

B.setprice (price);

B.setremark (remark);

return b; }

private static list<book> getbooklist (Document doc) {

@SuppressWarnings ("Unchecked")

list<element> nodeList = doc.selectnodes ("/books/book");

System.out.println (nodeList);

list<book> result = new linkedlist<> ();

Nodelist.foreach (e, {book b = Tobook (e);   Result.add (b); });

return result; } }

Java querying XML elements using dom4j

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.