尝试清除xpath的响应以摆脱xml
当我将字符串声明如下时,我的工作正常
String xml = "<section><title>Result</title><sectioncontents>2:58:37 am GMT/IST | Friday, December 6, 2013</sectioncontents></section>";
但当我宣布为
时 String response = WOLFRAM_ALPHA.query(s1);
我收到以下错误
[Fatal Error] :1:103: The markup in the document following the root element must be well-formed.
Exception in thread "main" org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:247)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:287)
at haha.main(haha.java:53)
字符串xml =“结果2:58:37 GMT / IST | 2013年12月6日星期五”;
这有效
InputSource source = new InputSource(new StringReader(xml));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(source);
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
String msg = xpath.evaluate("/section/title", document);
String status = xpath.evaluate("/section/sectioncontents", document);
System.out.println("msg=" + msg + ";" + "status=" + status);
这不是
WolframAlpha WOLFRAM_ALPHA = new WolframAlpha("L9PLQU-4L*****URY9");
// declare the scanner
Scanner input = new Scanner(System.in);
//prompt the user to ask a question
System.out.println("Please enter a query for wolfram");
//read in the question and store it in string s1
String s1 = input.nextLine();
String response = WOLFRAM_ALPHA.query(s1);
System.out.println(response);
InputSource res = new InputSource(new StringReader(response));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(res);
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
String msg = xpath.evaluate("/section/title", document);
System.out.println(msg);