Mahender Singh
Mahender Singh

Reputation: 1421

xml search element by id

Can we search a element by id in xml file using dom parser, for example :

<root>
  <context id="one">
    <entity>
      <identifier>new one</identifier>
    </entity>
  </context>

  <context id="two">
    <entity>
      <identifier>second one</identifier>
    </entity>
  </context>

</root>

I want a node with id = "one", my code

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document document = docBuilder.parse(new File("filename.xml"));

Element ele = document.getElementById("one");

return null,

is there any other way?

Upvotes: 3

Views: 7159

Answers (4)

Ashish Shetkar
Ashish Shetkar

Reputation: 1467

try and use XML - Xpath expression it is very easy

File fXmlFile = new File("filePath");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);


doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());


XPathFactory factory = XPathFactory.newInstance();

            XPath xpath = factory.newXPath();

            String expression;
            Node node;

            // 1. elements with id '1'
            expression = "//context[@id='one']"; 
            node = (Node ) xpath.evaluate(expression, doc, XPathConstants.NODE);

Upvotes: 0

bdoughan
bdoughan

Reputation: 149047

You could use the javax.xml.xpath APIs in the JDK/JRE to find the element by XPath.

Example

import java.io.File;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
import org.w3c.dom.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document document = docBuilder.parse(new File("filename.xml"));

        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();
        Element element = (Element) xpath.evaluate("//*[@id='one']", document, XPathConstants.NODE);
    }

}

Upvotes: 3

zEro
zEro

Reputation: 1263

You could instead use a third party library like Jsoup that does the job rather quite well.

File input = new File("/tmp/input.xml");
Document doc = Jsoup.parse(input, "UTF-8", "test");

And then you could use something like this:

doc.select("context#id=one")

Does that answer your question?

Upvotes: 0

wobblycogs
wobblycogs

Reputation: 4093

From the documentation for Document.getElementById

Note: Attributes with the name "ID" or "id" are not of type ID unless so defined.

The problem is the Document doesn't know that an attribute called id is an identifier unless you tell it. You need to set a schema on the DocumentBuilderFactory before you call newDocumentBuilder. That way the DocumentBuilder will be aware of the element types.

In the schema you will need something like this in the appropriate place:

<xs:attribute name="id" type="xs:ID"/> 

Upvotes: 3

Related Questions