0

I have an XML like the following,

<?xml version="1.0" encoding="utf-8"?>
    <PaymentElement>
        <Payment seqID="3">
            <TPayment>
                <Status>status</Status>
            </TPayment>
        </Payment>
    </PaymentElement>

The question is how do I retrieve/extract seqID value which is 3 out of this via java.

I have tried the following way, but it doesn't work.

InputStream xml = conn.getInputStream(); 
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xml);
NodeList list = doc.getElementsByTagName("PaymentElement");

for(int i=0; i<=list.getLength();i++){
    NodeList paySeq=doc.getElementsByTagName("Payment seqID");
System.out.println("Payment seqID"+paySeq);
}

2 Answers 2

1

try

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new File("1.xml"));
    Element e  = (Element)doc.getDocumentElement().getElementsByTagName("Payment").item(0);
    String id = e.getAttribute("seqID");
    System.out.println("Payment seqID = " + id);

output

 Payment seqID = 3
Sign up to request clarification or add additional context in comments.

1 Comment

I copypasted the full version in the answer, try again
1
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("/PaymentElement/Payment/@seqID");
Object result = expr.evaluate(doc, XPathConstants.STRING);

result should be having 3 now.

Full Example

import java.io.*;
import javax.xml.xpath.*;
import org.xml.sax.InputSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        InputStream inputStream = new FileInputStream("sample.xml");
        InputSource inputSource = new InputSource(inputStream);
        XPath xpath = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xpath.compile("/PaymentElement/Payment/@seqID");
        Object result = expr.evaluate(inputSource, XPathConstants.STRING);
        System.out.println(result);
    }

}

4 Comments

result is blank value 3 is not coming
public static void main(String[] args) throws Exception { InputStream xml = new FileInputStream("sample.xml"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(xml); XPath xpath = XPathFactory.newInstance().newXPath(); XPathExpression expr = xpath.compile("/PaymentElement/Payment/@seqID"); Object result = expr.evaluate(doc, XPathConstants.STRING); System.out.println(result); }
@RKajaMohideen - +1, I also added a modified version of your comment to your answer. The change I made was to eliminate the conversion to DOM.
@BlaiseDoughan That's perfectly fine. I just provided the solution for the issue he's trying to solve. I didn't concentrate on DOM part :-)

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.