80 likes | 281 Views
王豐緒 資訊工程學系. 補充 教材 : JAVA XPATH. 執行 XPATH 的 JAVA 方法. XPathAPI 類別 selectNodeIterator JAXP XPath 類別 XPathExpression 類別. 利用 JAXP 執行 XPATH 查詢 (1). // 載入 XML 文件 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); factory.setNamespaceAware (true); // never forget this!
E N D
王豐緒 資訊工程學系 補充教材:JAVA XPATH
執行XPATH的JAVA方法 • XPathAPI類別 • selectNodeIterator • JAXP • XPath類別 • XPathExpression類別
利用JAXP執行XPATH查詢(1) //載入XML文件 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); // never forget this! DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(“xxx.xml”); //xxx.xml是要查詢的xml文件檔名 //產生XPATH物件 XPathFactory factory = XPathFactory.newInstance(); XPathxpath = factory.newXPath(); XPathExpressionexpr = xpath.compile(“xpath運算式”);//假設此查詢會經常用到Object result = expr.evaluate(doc, XPathConstants.NODESET);//傳回NodeList //對查詢結果進行處理 NodeList nodes = (NodeList) result; //強制轉換成NodeList類別 for (inti = 0; i < nodes.getLength(); i++) { System.out.println(nodes.item(i).getNodeValue()); }
程式範例(1) import java.io.IOException; import org.w3c.dom.*; import org.xml.sax.SAXException; import javax.xml.parsers.*; import javax.xml.xpath.*; public class XpathExample { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { DocumentBuilderFactorydomFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); // never forget this! DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse("專案v4.xml"); XPathFactory factory = XPathFactory.newInstance(); XPathxpath = factory.newXPath(); XPathExpressionexpr = xpath.compile("//院長/姓名/text()"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (inti = 0; i < nodes.getLength(); i++) { System.out.println(nodes.item(i).getNodeValue()); } } }
XPATH 1.0資料模型 • NODESET • 例如: //作者 • XPathConstants.NODESET(org.w3c.dom.NodeList) • (XPathConstants.NODE: 只取第1個節點) • NUMBER • 例如:count(//作者) • XPathConstants.NUMBER(java.lang.Double) • BOOLEAN • 例如:count(//作者)>5 • XPathConstants.BOOLEAN(java.lang.Boolean) • STRING • XPathConstants.STRING(java.lang.String)
處理名稱空間的查詢 (1) • 搭配NamespaceContext類別 import java.util.Iterator; import javax.xml.*; import javax.xml.namespace.NamespaceContext; public class PersonalNamespaceContextimplements NamespaceContext { public String getNamespaceURI(String prefix) { if (prefix == null) throw new NullPointerException("Null prefix"); else if ("pre".equals(prefix)) return "http://www.example.com/books"; else if ("xml".equals(prefix)) return XMLConstants.XML_NS_URI; return XMLConstants.NULL_NS_URI; } }
處理名稱空間的查詢 (2) XPathFactory factory = XPathFactory.newInstance(); XPathxpath = factory.newXPath(); xpath.setNamespaceContext(new PersonalNamespaceContext()); XPathExpressionexpr = xpath.compile(“含有名稱空間前置詞的XPATH查詢"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (inti = 0; i < nodes.getLength(); i++) { System.out.println(nodes.item(i).getNodeValue()); }
結語 • 學會使用Xpath類別進行XPATH查詢 • 學會搭配NamespaceContext類別進行含有名稱空間前置詞的XPATH詢