0

I have an String XML. I need to get item ISSUCCESS value from XML in java. But item value return null. Here is my XML Code.

  1. XML

    <QRYRESULT><ISSUCCESS>N</ISSUCCESS><REASON>E002</REASON><WARNING>1. Transaction date Should be current date.11-SEP-18  E </WARNING></QRYRESULT>
    
  2. I have written bellow code to get ISSUCCESS item value.

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(**myXML**));
    Document doc = db.parse(is);
    NodeList nodeList = doc.getElementsByTagName("QRYRESULT");
    for (int i = 0; i < nodeList.getLength(); i++) {
        Element value = (Element) nodeList.item(i);
        String ISSUCCESS = value.getAttribute("ISSUCCESS");
        System.out.println("ISSUCCESS = " + ISSUCCESS);
    }
    
6
  • What is the Problem with your code? ISSUCCESS is not an Attribute. Commented Sep 12, 2018 at 9:39
  • @Jens ISSUCCESS item It returns null Commented Sep 12, 2018 at 9:39
  • @Jens I want to get ISSUCCESS item value Commented Sep 12, 2018 at 9:41
  • As far as I can see, there are no attributes for ISSUCCESS Commented Sep 12, 2018 at 9:42
  • @MadProgrammer how to get ISSUCCESS item value Commented Sep 12, 2018 at 9:44

5 Answers 5

3

ISSUCCESS is an Element not an attribute.

changeTo:

NodeList nodeList = doc.getElementsByTagName("QRYRESULT");
for (int i = 0; i < nodeList.getLength(); i++) {
    Element value = (Element) nodeList.item(i);

    String ISSUCCESS = value.getElementsByTagName("ISSUCCESS").item(0).getTextContent();
    System.out.println("ISSUCCESS = " + ISSUCCESS);
}
Sign up to request clarification or add additional context in comments.

Comments

0

There are multiple ways to do that:

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new InputSource(new StringReader(myXML)));
    // 1, get root element, get first child (ISSUCCESS), get first child (text node), get value
    System.out.println(doc.getDocumentElement().getFirstChild().getFirstChild().getNodeValue());
    // 2, using nodelist
    NodeList rootChildNodes = doc.getDocumentElement().getChildNodes();
    String ISSUCCESSValue = null; 
    for (int i = 0; i < rootChildNodes.getLength(); i++) {
        Node childNode = rootChildNodes.item(i);
        if("ISSUCCESS".equals(childNode.getNodeName())){
            // get text node and get the value
            ISSUCCESSValue = childNode.getFirstChild().getNodeValue();
            break;
        }
    }   
    System.out.println(ISSUCCESSValue);
    // 3, using XPATH
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr = xpath.compile("/QRYRESULT/ISSUCCESS/text()") // or xpath.compile("//ISSUCCESS/text()");
    String value = expr.evaluate(doc.getDocumentElement());
    System.out.println(value);

Comments

0

You can get ISSUCCESS item value using below code:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(**myXML**));
Document doc = db.parse(is);
NodeList nodeList = doc.getElementsByTagName("QRYRESULT");
for (int i = 0; i < nodeList.getLength(); i++) {
    Node childNode = childList.item(i);
    if ("ISSUCCESS".equals(childNode.getNodeName())) {
        System.out.println(childList.item(i).getTextContent()
                                    .trim());
    }
}

Comments

0
DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(**myXML**));
Document doc = db.parse(is);
NodeList nodes = doc.getElementsByTagName("QRYRESULT");
Element qryresult = (Element) nodes.item(0);
Element success = (Element) qryresult.getElementsByTagName("ISSUCCESS").item(0);
String iSuccess=success.getFirstChild().getTextContent();
System.out.println("ISSUCCESS: " + iSuccess);

Comments

0

Try this one.

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(contactsXMLstream);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("QRYRESULT");
entries=new MarketEntry[nList.getLength()];            
for (int temp = 0; temp < nList.getLength(); temp++)
{
    Node nNode = nList.item(temp);
    if (nNode.getNodeType() == Node.ELEMENT_NODE)
    {
        Element eElement = (Element) nNode;
        String vr= eElement.getElementsByTagName("ISSUCCESS").item(0).getTextContent();
    }
}

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.