Presentation is loading. Please wait.

Presentation is loading. Please wait.

 能夠利用 XPATH 與 Xalan 套件,撰寫 JAVA 程 式,進行 XML 文件的查詢功能.

Similar presentations


Presentation on theme: " 能夠利用 XPATH 與 Xalan 套件,撰寫 JAVA 程 式,進行 XML 文件的查詢功能."— Presentation transcript:

1

2  能夠利用 XPATH 與 Xalan 套件,撰寫 JAVA 程 式,進行 XML 文件的查詢功能

3 import org.apache.xpath.XPathAPI; Import org.w3c.dom.traversal.*; //for NodeIterator public class ApplyXPath { public void xxx() { String xpath = ….; // Set up a DOM tree to query. Document doc ….. // Use the simple XPath API to select a nodeIterator. NodeIterator nl = XPathAPI.selectNodeIterator(doc, xpath); Node n; while ((n = nl.nextNode()) != null) { if (isTextNode(n)) { // DOM may have more than one node corresponding to a single XPath text node. // Coalesce all contiguous text nodes at this level StringBuffer sb = new StringBuffer( n.getNodeValue() ); for (Node nn = n.getNextSibling(); isTextNode(nn);nn = nn. getNextSibling ()) { sb.append(nn. getNodeValue ()); } System.out.print(sb); } ….. }

4 Sample4 1. Files: Sample4.java testdata.xml xalan.jar serilizer.jar 2. Compile javac -classpath xalan.jar;serilizer.jar Sample4.java 3. Run java -classpath.;xalan.jar;serilizer.jar Sample4 // 範例 操作說明

5 import org.apache.xpath.XPathAPI; Import org.w3c.dom.traversal.*; //for NodeIterator // 建立一個含有 namespace 的節點 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); DOMImplementation impl = builder.getDOMImplementation(); Document namespaceHolder = impl.createDocument("", "namespaceMapping", null); Element namespaceND = namespaceHolder.getDocumentElement(); String q_name = “xmlns:mcu”; String ns = “http://www.mcu.edu.tw”; namespaceND.setAttributeNS("http://www.w3.org/2000/xmlns/", q_name, ns); Node context_node = …. String xpath = …. NodeIterator nl =XPathAPI.selectNodeIterator(context_node, xpath, namespaceND ); 含有 namespace 宣告的元素 xmlns:mcu=“http://www.mcu.edu.tw”

6 Sample5 1. Files: Sample5.java testdata.xml xalan.jar serilizer.jar 2. Compile javac -classpath xalan.jar;serilizer.jar Sample5.java 3. Run java -classpath.;xalan.jar;serilizer.jar Sample5 //mcu: 範例 操作說明

7  歸屬於 org.apache.xpath 套件  提供 6 個 XPATH 查詢功能 › Node selectSingleNode (Node contextNode, String str) › Node selectSingleNode (Node contextNode, String str, Node namespaceNode) › NodeIterator selectNodeIterator (Node contextNode, String str) › NodeIterator selectNodeIterator (Node contextNode, String str, Node namespaceNode) › NodeList selectNodeList (Node contextNode, String str) › NodeList selectNodeList (Node contextNode, String str, Node namespaceNode)

8  介紹 XPathAPI 類別  XPathAPI 類別在 XPATH 查詢的應用


Download ppt " 能夠利用 XPATH 與 Xalan 套件,撰寫 JAVA 程 式,進行 XML 文件的查詢功能."

Similar presentations


Ads by Google