发布于 2016-07-12 12:37:33 | 125 次阅读 | 评论: 0 | 来源: 网友投递
Java程序设计语言
java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。
import java.io.IOException;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
public class XpathTest {
public static void main(String[] args) throws ParserConfigurationException,
SAXException, IOException, XPathExpressionException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("C:/Users/Administrator/Desktop/test.xml");
System.out.println(doc.getChildNodes().getLength());
XPathFactory xFactory = XPathFactory.newInstance();
XPath xpath = xFactory.newXPath();
XPathExpression expr = xpath
.compile("//name/text()");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
System.out.println(nodes.getLength());
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
}
}
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="eng">Learning XML</title>
<price>39.95</price>
</book>
</bookstore>
public Object evaluate(Object item, QName returnType)throws XPathExpressionException;
public String evaluate(Object item)throws XPathExpressionException;
public Object evaluate(InputSource source, QName returnType)throws XPathExpressionException;
public String evaluate(InputSource source)throws XPathExpressionException;
public Object evaluate(Object item, QName returnType)throws XPathExpressionException;
public String evaluate(Object item)throws XPathExpressionException;
public Object evaluate(InputSource source, QName returnType)throws XPathExpressionException;
public String evaluate(InputSource source)throws XPathExpressionException;
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expression = xpath.compile("/bookstore//book/title/text()");
<strong><strong> XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expression = xpath.compile("/bookstore//book/title/text()");</strong></strong>
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(new File("books.xml"));
NodeList list = (NodeList) expression.evaluate(document,XPathConstants.NODESET);
<strong><strong> DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(new File("books.xml"));
NodeList list = (NodeList) expression.evaluate(document,XPathConstants.NODESET);</strong></strong>
for(int i = 0;i<list.getLength();i++){ System.out.println(list.item(i).getNodeValue());
}
<strong><strong> for(int i = 0;i</strong></strong>
Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML
<strong><strong>Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML</strong></strong>
<strong><strong>
Hello
</strong></strong>
<?xml version="1.0" encoding="UTF-8"?>
<tg:bookstore xmlns:tg="http://www.tibco.com/cdc/liugang"
xmlns:ns="http://www.tibco.com/cdc/liugang/ns">
<ns:book>
<tg:title>Hello</tg:title>
</ns:book>
</tg:bookstore>
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
XPathExpression compile = xpath.compile("//*[local-name()='book']");
NodeList list = (NodeList) compile.evaluate(document,XPathConstants.NODESET);
<strong><strong> XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
XPathExpression compile = xpath.compile("//*[local-name()='book']");
NodeList list = (NodeList) compile.evaluate(document,XPathConstants.NODESET);</strong></strong>
<?xml version="1.0" encoding="UTF-8"?>
<bookstore xmlns="http://www.tibco.com/cdc/liugang" xmlns:tg="http://www.tibco.com/cdc/liugang/tg"
xmlns:ns="http://www.tibco.com/cdc/liugang/ns">
<ns:book>
<tg:title>Hello</tg:title>
</ns:book>
<computer>
<id>ElsIOIELdslke-1233</id>
</computer>
</bookstore>
<strong><strong>
Hello
ElsIOIELdslke-1233
</strong></strong>
class CustomNamespaceContext implements NamespaceContext{
public String getNamespaceURI(String prefix) {
if(prefix.equals("ns")){
return "http://www.tibco.com/cdc/liugang/ns";
}else if(prefix.equals("tg")){
return "http://www.tibco.com/cdc/liugang/tg";
}else if(prefix.equals("df")){
return "http://www.tibco.com/cdc/liugang";
}
return XMLConstants.NULL_NS_URI;
}
public String getPrefix(String namespaceURI) {
return null;
}
public Iterator getPrefixes(String namespaceURI) {
return null;
}
}
<strong><strong>class CustomNamespaceContext implements NamespaceContext{
public String getNamespaceURI(String prefix) {
if(prefix.equals("ns")){
return "http://www.tibco.com/cdc/liugang/ns";
}else if(prefix.equals("tg")){
return "http://www.tibco.com/cdc/liugang/tg";
}else if(prefix.equals("df")){
return "http://www.tibco.com/cdc/liugang";
}
return XMLConstants.NULL_NS_URI;
}
public String getPrefix(String namespaceURI) {
return null;
}
public Iterator getPrefixes(String namespaceURI) {
return null;
}
}</strong></strong>
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
xpath.setNamespaceContext(new CustomNamespaceContext());
XPathExpression compile = xpath.compile("//df:computer");
NodeList list = (NodeList) compile.evaluate(document,XPathConstants.NODESET);
for(int i = 0;i
Node item = list.item(i);
System.out.println(item.getNodeName()+" "+item.getNodeValue());
}
<strong><strong> XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
xpath.setNamespaceContext(new CustomNamespaceContext());
XPathExpression compile = xpath.compile("//df:computer");
NodeList list = (NodeList) compile.evaluate(document,XPathConstants.NODESET);
for(int i = 0;i</strong></strong>
/**
*
Establish a variable resolver.
*
*
A NullPointerException is thrown if resolver is null.
*
* @param resolver Variable resolver.
*
* @throws NullPointerException If resolver is null.
*/
public void setXPathVariableResolver(XPathVariableResolver resolver);
/**
*
Establish a function resolver.
*
*
A NullPointerException is thrown if resolver is null.
*
* @param resolver XPath function resolver.
*
* @throws NullPointerException If resolver is null.
*/
public void setXPathFunctionResolver(XPathFunctionResolver resolver);
<strong><strong> /**
* Establish a variable resolver.
*
* A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.
*
* @param resolver Variable resolver.
*
* @throws NullPointerException If <code>resolver</code> is <code>null</code>.
*/
public void setXPathVariableResolver(XPathVariableResolver resolver);
/**
* Establish a function resolver.
*
* A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.
*
* @param resolver XPath function resolver.
*
* @throws NullPointerException If <code>resolver</code> is <code>null</code>.
*/
public void setXPathFunctionResolver(XPathFunctionResolver resolver);</strong></strong>