Working with DOM4J XML (3)

Source: Internet
Author: User

Today I want to use DOM4J to select the node, by the way, there is no separate way to go heavy, but there is a can go heavy, but also must provide an XPath as the basis for sorting and de-weight.

1. xml file

<?XML version= "1.0" encoding= "UTF-8"?><Tasks>    <Taskname= "Work"Delay= "0"period= "43200"></Task>    <Taskname= "Sleep"Delay= "3600"period= "43200"></Task>    <Taskname= "Play"Delay= "+"period= "604800"></Task>    <Taskname= "Play"Delay= "+"period= "604800"></Task></Tasks>

2, the method and parameters used to explain:

The SelectNodes has 3 overloaded methods, one parameter, two parameters, and three parameters.

Selectnode (String xpath1, String xpath2, String boolval)

(1) Xpath1: Used to select a node in the text and return a result set (list<node>).

(2) Xpath2: The point of view of a node in the result, pointing out the basis of sorting and de-weight.

(3) 0boolVal: When the parameter is true, the selected result is de-processed.

Uh...... Seems a little difficult to understand, see examples.

3. Test code and results

Document doc = new Saxreader (). Read (New File ("Src/operxml/xmlfiles/tasks.xml"));

01, select all the task nodes, according to the name attribute of the node to sort, the result is heavy. true-System.out.println (Tn.asxml ()));

Results:

<Taskname= "Play"Delay= "+"period= "604800"/><Taskname= "Sleep"Delay= "3600"period= "43200"/><Taskname= "Work"Delay= "0"period= "43200"/>

// 02. Get all task names, sort by task name and go to weight // list<node> tasknames = Doc.selectnodes ("/ /// How to select a single node.  trueTasknames.foreach (TN---SYSTEM.OUT.PRINTLN (Tn.asxml ()));

Results:

Name= "Play" Name= "Sleep" name= "work"

// 03. Get all the task names, sort by delay and go to heavy true // The second parameter is sorted by, de-re-based (obviously ordered by delay; compared to 02:02 has the same name, only one after the go-back, and 03 in delay. )Tasknames.foreach (TN-SYSTEM.OUT.PRINTLN (Tn.asxml ()));

Results:

Name= "Work" name= "Play" name= "Play" Name= "Sleep"

4. Gossip:

Originally wanted to paste the code of the Anti-compilation, later code see Finally, there is a new 1 (this, sortvalues). What a puzzle! Later downloaded the Jd-gui, open the dom4j jar package, the code is not like that at all. Forget it, don't plan to analyze it again.

However, this is called Enhanced Class Decompiler Eclipse plug-in code, the format is really not flattering, anti-compiled code and Jd-gui will be different.

]

Working with DOM4J XML (3)

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.