Jdom use XPath to find nodes with namespace

Source: Internet
Author: User
Tags xmlns xpath

For nodes with no namespaces (NameSpace) in the XML file, such as

<blog_Content>

You can use JDOM XPath to search this node for any of the following code:

① List<Element> elements = XPath.selectNodes(document,"//blog_Content");

② XPath xpath = XPath.newInstance("//blog_Content");
  List<Element> elements = xpath.selectNodes(document);

But if you are looking for a node with a namespace, such as

<blogns:blog_content xmlns:blogns= "X-schema: #Schema2" >

<blogns:title>quartz Job scheduling framework[translation] Chapter I. Job scheduling </blogns:Title> in enterprise applications

..............................................

Or use the previous two methods can not find the Blog_content node, even if it is written

list<element> elements = xpath.selectnodes (document, "//blogns:blog_content");

or write about <blogns:Title>

list<element> elements = xpath.selectnodes (document, "//blogns:title");

The element is not retrieved, Elements.size () is 0.

At this point, we need to explicitly specify the NameSpace you want to use for your XPath instance, using the XPath AddNamespace () method, the Code of Action is as follows:

XPath xpath = XPath.newInstance("//blogns:blog_Content");
   xpath.addNamespace("blogns","x-schema:#Schema2");
   List<Element> elements = xpath.selectNodes(document);

This will be able to retrieve the <blogns:blog_Content> node, and for <blogns:Title> is the same practice.

But how do you retrieve a node that uses the default namespace (Namespace)? For example, retrieve the <blog_Content> node of the following XML

<blog_content xmlns= "X-schema: #Schema2" >

<title>quartz Job scheduling framework[translation] Chapter I. Job scheduling </Title> in enterprise applications

.................................

In a more conventional way of thinking, the Default Namespace is probably "" empty string, then is not written

XPath xpath = XPath.newInstance("//blog_Content");
   xpath.addNamespace("","x-schema:#Schema2");
   List<Element> elements = xpath.selectNodes(document);

Can we find the node? Unfortunately, the elements.size () is equal to 0, and the retrieval fails.

JDOM requirements, I do not know whether all XML parsing APIs have such a specification, even if the use of the default namespace, in the XPath retrieval must give this default namespace to specify a name, we choose the "Default" bar, you can arbitrarily specify a name that does not conflict, Don't be misled by the "default" here, just use it. So the code to retrieve <blog_Content> is:

XPath xpath = XPath.newInstance("//default:blog_Content");
xpath.addNamespace("default","x-schema:#Schema2");
List<Element> elements = xpath.selectNodes(document);

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.