Use XPath to parse XML documents (2)

Source: Internet
Author: User

Use JDOM xpath Class ParsingThe jdom api class supports XPath expressions to locate nodes from XML documents. Some methods of the JDOM XPath class are listed in the following table:
XPath Class Method Description
Selectsinglenode It is used to match XPath to locate a single node.
Selectnodes It is used to match XPath to locate the node set.
Addnamespace Add a namespace to match the XPath with the namespace prefix.
In this section, use the JDOM XPath class Catalog. xml The process of locating nodes will be discussed. Quilt Select The node for method locating is modified, and the modified document is output XML Documentation. First, import JDOM PackageOrg. JDOM. xpathimport org. JDOM. XPath. *; create a saxbuildersaxbuilder saxbuilder = new saxbuilder ("org. Apache. xerces. parsers. saxparser"). parse XML documents with saxbuilder Catalog. xml :Org. JDOM. Document jdomdocument = saxbuilder. Build (xmldocument); xmldocument is an XML document Catalog. xml OfJava. Io. File Static MethodSelectsinglenode (Java. Lang. object context, string xpathexpression) uses a specific XPath expression to locate a single node. If more than one node matches the XPath expression, the first node is selected. Use XPath to locate a journal article element attribute node level, set the journal attribute node title to Java technology, and set the article element attribute node date to January-2004. Org. JDOM. attribute levelnode = (Org. JDOM. attribute) (XPath. selectsinglenode (jdomdocument, "/CATALOG // Journal [@ Title = 'javatechnology ']" + "// article [@ date = 'january-000000']/@ level ")); the Level Attribute Value advanced is located and the level node is modified. Levelnode. setvalue ("intermediate"); The selectsinglenode method can also be used to locate nodes in XML documents. As an example, select a title node and use XPath to locate the node. Org. JDOM. element titlenode = (Org. JDOM. element) XPath. selectsinglenode (jdomdocument, "/CATALOG // Journal // article [@ date = 'january-000000']/Title "); the title node Value Design Service-Oriented Architecture frameworks with J2EE technology is located and the title node is modified. Titlenode. settext ("Service Oriented Architecture frameworks"); select all nodes specified by XPath in the static method selectnodes (Java. Lang. object context, string xpathexpression. Locate all Article nodes of journal and set the attribute title of journal to Java technology. Java. util. list nodelist = XPath. selectnodes (jdomdocument, "/CATALOG // Journal [@ Title = 'java technology '] // article"); Modify the article node and add attributes to the article node. Iterator iter = nodelist. iterator (); While (ITER. hasnext () {Org. JDOM. element element = (Org. JDOM. element) ITER. next (); element. setattribute ("section", "Java technology");} The JDOM XPath class supports locating nodes with namespaces prefix. Locate the node with a namespace and add the namespace to XPath: XPath = XPath. newinstance ("/CATALOG // Journal: Journal // article/@ Journal: Level"); XPath. addnamespace ("journal", "http://www.w3.org/2001/XMLSchema-Instance"); As above, the namespace prefixed with journal is added to the XPath object. Locate the node that contains the namespace prefix. Levelnode = (Org. JDOM. Attribute) XPath. selectsinglenode (jdomdocument); attribute node Journal: level is located. Modify this node: levelnode. setvalue ("advanced "); Java Program Jdomparser. Java Used from XML Document Catalog. xml Select a node. This section describes how to use JDOM Of Xpath Class Select Method from XML The process of locating nodes in the document. The node to be located is modified, and the modified document passesThe xmloutputter class outputs an XML document. Catalog-modified.xml This is the output XML Documentation. Conclusion In this tutorial Xpath Resolved XML Documentation. Xpath It is only used to locate nodes. As discussed in this tutorial XPath API Not used Xpath Is XML Document to modify the node value. To modify the value of a node,The setter method of the org. W3C. Dom package is required.

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.