我正在尝试从XML中的嵌套标记中读取值并且遇到问题。
我的XML文件名为 NewFile.xml 我正在尝试读取tag4中的值。
<?xml version="1.0"?>
<tag1>
<tag2>
<tag3>
<tag4>read me</tag4>
</tag3>
</tag2>
</tag1>
这是一个名为 Test.java 的主类,它读取该XML文件并将其转换为字符串,并在读者类中向其发送名为lookUpValue的方法。
public class test {
public static void main(String [] args) throws IOException, XPathExpressionException
{
BufferedReader br = new BufferedReader(new FileReader(new File("NewFile.xml")));
String xmlString;
StringBuilder sb = new StringBuilder();
while((xmlString=br.readLine())!= null){
sb.append(xmlString.trim());
}
xmlString = sb.toString();
System.out.print(reader.LookUpValue(xmlString));
br.close();
}
}
这是读者类和方法LookupValue:
import java.io.StringReader;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.xml.sax.InputSource;
public class reader{
public static String LookUpValue(String sb) throws XPathExpressionException
{
InputSource source = new InputSource(new StringReader(sb));
XPath xpath = XPathFactory.newInstance().newXPath();
Object customer = xpath.evaluate("/tag1/tag2/tag3", source, XPathConstants.NODE);
String value = xpath.evaluate("tag4", customer);
return ("Value is: " + value);
}
}
我的问题是如何在运行此程序时立即读取tag4中的值我得到以下错误。
Exception in thread "main"
javax.xml.transform.TransformerException: Unable to evaluate expression using this context
at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:363)
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(XPathImpl.java:213)
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:275)
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:365)
at reader.LookUpValue(LookUpValue.java:21)
at test.main(test.java:26)
Caused by: java.lang.RuntimeException: Unable to evaluate expression using this context
at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(NodeSequence.java:212)
at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(LocPathIterator.java:210)
at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:335)
... 5 more
---------
java.lang.RuntimeException: Unable to evaluate expression using this context
at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(NodeSequence.java:212)
at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(LocPathIterator.java:210)
at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:335)
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(XPathImpl.java:213)
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:275)
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:365)
at reader.LookUpValue(LookUpValue.java:21)
at test.main(test.java:26)
--------------- linked to ------------------
javax.xml.xpath.XPathExpressionException
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:289)
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:365)
at reader.LookUpValue(LookUpValue.java:21)
at test.main(test.java:26)
Caused by: javax.xml.transform.TransformerException: Unable to evaluate expression using this context
at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:363)
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(XPathImpl.java:213)
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:275)
... 3 more
Caused by: java.lang.RuntimeException: Unable to evaluate expression using this context
at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(NodeSequence.java:212)
at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(LocPathIterator.java:210)
at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:335)
... 5 more
答案 0 :(得分:1)
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
public class reader {
public static String LookUpValues(String sb) throws Exception{
DocumentBuilder builder = DocumentBuilderFactory
.newInstance().newDocumentBuilder();
InputSource src = new InputSource();
src.setCharacterStream(new StringReader(sb));
Document doc = builder.parse(src);
String value = doc.getElementsByTagName("tag4").item(0).getTextContent();
return ("Value is: " + value);
}
}