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.
XML
<QRYRESULT><ISSUCCESS>N</ISSUCCESS><REASON>E002</REASON><WARNING>1. Transaction date Should be current date.11-SEP-18 E </WARNING></QRYRESULT>
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); }
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);
}
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());
}
}
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();
}
}
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);
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);
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.