jesric1029
jesric1029

Reputation: 718

Unexpected results from Java DOM getAttribute

I am having some trouble with my DOM parser for Java. I am trying to parse the entire XML document and save everything into either Strings or ArrayLists depending how it appears in the document. Right now I have run into one issue in particular. I am trying to parse the attribute of a child value of an element here:

-<Ntfctn>

      <Id>161RD2521JI00XSZ</Id>

      <CreDtTm>2016-01-27T13:25:21</CreDtTm>

      +<Acct>

      +<TxsSummry>

      +<Ntry xmlns="urn:iso:std:iso:20022:tech:xsd:camt.054.001.04">

As you can see there are two elements under Ntfctn that contain attributes, Id and CreDtTm. Right now all I really need is "Id". I have already collected the credit amount earlier in my code. There are numerous "Id" tags throughout the XML so calling "Id" directly won't work.

I have tried calling the attribute in this way:

NodeList Ntfctn = doc.getElementsByTagName("Ntfctn");

            for(int temp = 0; temp < MsgRcpt.getLength(); temp++){
                System.out.println(Ntfctn.item(0).getAttributes());
            }

But get some unexpected results

com.sun.org.apache.xerces.internal.dom.AttributeMap@2a139a55

I've tried changing the Ntfctn.get value to several numbers but none seem to pull what I am looking for. Other areas of my code have worked perfectly. I will post the entire thing below.

Please let me know the proper way for me to get the value from an attribute that has the same name across the file.

package parsing;

import java.io.File;
import org.w3c.dom.*;

import javax.xml.parsers.*;
import java.io.*;

public class XMLParser 

{

    public static String CreDtTmS;
    public static String MsgIdS; 
    public static String MsgRcptS;
    public static String NtfctnS;

    public void parseXML(){

        try{

            File inputFile = new File("C:\\Users\\jhamric\\Desktop\\Camt54.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(inputFile);
            doc.getDocumentElement().normalize();

            NodeList CreDtTm = doc.getElementsByTagName("CreDtTm");

            for(int temp = 0; temp < CreDtTm.getLength(); temp++){
                CreDtTmS = CreDtTm.item(0).getTextContent();
            }

            NodeList MsgId = doc.getElementsByTagName("MsgId");

            for(int temp = 0; temp < MsgId.getLength(); temp++){
                MsgIdS = MsgId.item(0).getTextContent();
            }

            NodeList MsgRcpt = doc.getElementsByTagName("MsgRcpt");

            for(int temp = 0; temp < MsgRcpt.getLength(); temp++){
                MsgRcptS = MsgRcpt.item(0).getTextContent();
            }

            NodeList Ntfctn = doc.getElementsByTagName("Ntfctn");

            for(int temp = 0; temp < MsgRcpt.getLength(); temp++){
                System.out.println(Ntfctn.item(0).getAttributes());
            }


        }catch (Exception e){
            e.printStackTrace();
        }

        System.out.println("GrpHdr -> CreDtTm: "+CreDtTmS);
        System.out.println("GrpHdr -> MsgId: "+MsgIdS);
        System.out.println("GrpHdr -> MsgRcpt -> Id -> OrgId -> Othr -> Id: "+MsgRcptS);


    }

}

The question was answered using help from the accepted answer below.

After trial and error this was the bit of code that worked:

XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();
            XPath xPath =XPathFactory.newInstance().newXPath();

            XPathExpression expr = xpath.compile("//Ntfctn/Id");
            Object result = expr.evaluate(doc, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;
            System.out.println(nodes.getLength());
            for(int i = 0; i < nodes.getLength(); i++){
                Element el = (Element) nodes.item(i);
                System.out.println("Tag: "+ el.getNodeName());
                System.out.println(el.getTextContent());
            }

Upvotes: 1

Views: 72

Answers (1)

hasnae
hasnae

Reputation: 2173

To refine your selection you can use xpath:

XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList)xPath.evaluate("//Ntfctn/Id",
        doc.getDocumentElement(), XPathConstants.NODESET);

Upvotes: 1

Related Questions