我有一个String XML。我需要从Java中的XML获取项目 ISSUCCESS 的值。但是项目值返回null。这是我的XML代码。
XML
<QRYRESULT><ISSUCCESS>N</ISSUCCESS><REASON>E002</REASON><WARNING>1. Transaction date Should be current date.11-SEP-18 E </WARNING></QRYRESULT>
我写了下面的代码来获取 ISSUCCESS 物品的价值。
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);
}
答案 0 :(得分:3)
ISSUCCESS是元素而不是属性。
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);
}
答案 1 :(得分:0)
您可以使用以下代码获取ISSUCCESS项目值:
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());
}
}
答案 2 :(得分:0)
尝试这个。
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();
}
}
答案 3 :(得分:0)
有多种方法可以做到这一点:
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);
答案 4 :(得分: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);