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);