0

We are trying to build a service that receive some XML files. But people send it, sometimes use namespaces, sometimes not. For example:

<?xml version="1.0" encoding="UTF-8"?>
<ds:EnvioDoc version="1.0" xmlns:ds="http://my.domain">
  <ds:Cabecera version="1.0">
    <ds:IdRec>215217190015</ds:IdRec>
    <ds:IdEm>211003420017</ds:IdEm>
    <ds:IdDoc>2995019</ds:IdDoc>
  </ds:Cabecera>
 <Cuerpo>
  <CorpDoc version="1.0" xmlns="http://my.domain"     xmlns:xd="http://www.w3.org/2000/09/xmldsig#">
  <body>
    <Fecha>2016-08-12T00:11:50-03:00</Fecha>
[..]

or can come like:

<?xml version="1.0" encoding="UTF-8"?>
<EnvioDoc version="1.0" xmlns="http://my.domain">
  <Cabecera version="1.0">
    <IdRec>215217190015</IdRec>
    <IdEm>211003420017</IdEm>
    <IdDoc>2995019</IdDoc>
  </Cabecera>
  <Cuerpo>
   <CorpDoc version="1.0" xmlns="http://my.domain"  xmlns:xd="http://www.w3.org/2000/09/xmldsig#">
    <body>
      <Fecha>2016-08-12T00:11:50-03:00</Fecha>
 [..]

We have tried to use this code to read this files:

        File edocFile = new File(fileName);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(edocFile);

        //recomendado http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
        doc.getDocumentElement().normalize();
    NodeList nlCabecera = doc.getElementsByTagNameNS("*","Cabecera");
    Node cabeceraNode = nlCabecera .item(0);
    if (cabeceraNode.getNodeType() == Node.ELEMENT_NODE) {

But getting a NullPointerExsception in the if line.

Any suggest?

TIA

4
  • 1
    If you want to work with namespaces then I would start with setting dbFactory.setNamespaceAware(true);. Commented Aug 18, 2016 at 15:13
  • 1
    Note also that both your samples use the same namespace, only as a default namespace in the second sample and with a prefix in the first. Commented Aug 18, 2016 at 15:14
  • Thanks a lot! It's working now! Commented Aug 18, 2016 at 15:46
  • I have put the suggestion into an answer so that you can mark the problem as solved. Commented Aug 18, 2016 at 15:53

2 Answers 2

3

To use namespace aware DOM methods like getElementsByTagNameNS you need to make sure you use a namespace aware DOM by setting dbFactory.setNamespaceAware(true); on your factory used to create the DOM parser/DocumentBuilder.

Sign up to request clarification or add additional context in comments.

Comments

0

One way you can get it to work with or without namespaces is as follows. Note that this solution uses XPATH , Although this does not solve the NPE you are getting the solution will work for the case with or without namespaces.

public static void main(String[] args) throws Exception {
    File fXmlFile = new File("C:\\DevelopmentTools\\3.CODE\\XMLWithNS.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
    doc.getDocumentElement().normalize();

    XPath xpath = XPathFactory.newInstance().newXPath();
    NodeList list = (NodeList) xpath.evaluate("//*[local-name()='Cabecera']", doc, XPathConstants.NODESET);

    System.out.println("Size of the list is " + list.getLength());

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.