简体   繁体   中英

Parse XML using DOM and print in java

I tried a lot parsing the following XML using DOM. I am not able to print the values of elements. Can anybody help me to print the values of the elements.

Thanks in advance

<tns:CustomerDetails xmlns:xe="http://www.w3.org/2001/04/xmlenc#" xmlns:xd="http://www.w3.org/2000/09/xmldsig#" xmlns:tns="http://abc.com/elements" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pf="http://abc.com/Service">
  <tns:CustomerId>360817</tns:CustomerId>
  <tns:CustomerName>ABC Corp</tns:CustomerName>
  <tns:PAN>awap</tns:PAN>
  <tns:Timestamp>2010-05-20T12:20:19Z</tns:Timestamp>
  <tns:RequestId>397</tns:RequestId>
  <tns:PIN>1234</tns:PIN>
</tns:CustomerDetails>

My code

      File infile = new File("D:\\Cust.xml");
  DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
  Document doc = dBuilder.parse(infile);
  doc.getDocumentElement().normalize();

  System.out.println("root of xml file " +
                     doc.getDocumentElement().getNodeName());
  System.out.println("==========================");

  NodeList list = doc.getElementsByTagName("CustomerDetails");
  System.out.println(list.getLength());
  Element name = doc.getElementById("CustomerId");
  if (name == null) {
    System.out.println("There is no element with the ID ");
  } else {
    Text text = (Text)name.getFirstChild();
    System.out.println("The ID " + " locates the name " + text.getData());
  }

I have tried with

Element name = doc.getElementById("tns:CustomerId");

also.. i get null while printing

You're mixing your models here. getElementById is for elements that have an attribute identified by the document's DTD as an ID, and since your document doesn't have a DTD it will never give you anything useful.

Because your document uses namespaces you should use the "NS" methods to extract elements, and for the text contained in an element you can use getTextContent

  File infile = new File("D:\\Cust.xml");
  DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  // namespaces - DocumentBuilderFactory is *not* namespace aware by default
  dbFactory.setNamespaceAware(true);
  DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
  Document doc = dBuilder.parse(infile);
  doc.getDocumentElement().normalize();

  System.out.println("root of xml file " +
                     doc.getDocumentElement().getNodeName());
  System.out.println("==========================");

  NodeList list = doc.getElementsByTagNameNS(
        "http://abc.com/elements", "CustomerDetails");
  System.out.println(list.getLength());
  for(int i = 0; i < list.getLength(); i++) {
    Element custDetails = (Element)list.item(i);
    Element id = custDetails.getElementsByTagNameNS(
        "http://abc.com/elements", "CustomerId").item(0);
    System.out.println("Customer ID: " + id.getTextContent());
    Element name = custDetails.getElementsByTagNameNS(
        "http://abc.com/elements", "CustomerName").item(0);
    System.out.println("Customer Name: " + name.getTextContent());
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM