直接解析出body節點中間的文本
<html>
<head>
<title>
test
</title>
</head>
<body algin="center" stly="windows.back()">
liangshi
</body>
</html>
-------------------程式如下-------------------------------------------------------------------------------
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class Xpath_text {
public static void main(String[] args) {
//得到工廠
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
//得到builder
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
//得到要解析的檔案
File f = new File("D://c++//test.xml");
Document doc = null;
try {
//得到樹
doc = builder.parse(f);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//得到XPath的工廠
XPathFactory xfactory = XPathFactory.newInstance();
//得到path
XPath path = xfactory.newXPath();
try {
//得到要解析出來的文本
String body = path.evaluate("/html/body", doc);
//在控制台列印出解析出來的結果
System.out.println("得到的結果是:"+body);
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
}
----------------------結果是如下---------------------------------------------------------------------
得到的結果是:
liangshi