<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Java XPath `NamespaceContext` – 命名空間解析示例 > 原文: [https://howtodoinjava.com/xml/xpath-namespace-resolution-example/](https://howtodoinjava.com/xml/xpath-namespace-resolution-example/) 在此 Java 示例中,我們將使用[`NamespaceContext`](https://docs.oracle.com/javase/7/docs/api/javax/xml/namespace/NamespaceContext.html)將 XPath 命名空間解析學習為 XML 文件,該文件具有命名空間聲明和各自的用法。 ## 添加了 XML 文件的命名空間 我已經創建了`sample.xml`文件并將其放在類路徑中。 ```java <ns2:bookStore xmlns:ns2="http://bookstore.com/schemes"> <ns2:book id="1"> <ns2:name>Data Structure</ns2:name> </ns2:book> <ns2:book id="2"> <ns2:name>Java Core</ns2:name> </ns2:book> </ns2:bookStore> ``` ## 實現`NamespaceContext`以創建命名空間解析器 該命名空間解析器可以與使用了命名空間定義的任何 XML 文件一起使用。 它搜索 XML 文檔本身內任何給定命名空間前綴(作為參數傳遞)的命名空間聲明。 因此**無需單獨創建命名空間映射**。 ```java public class NamespaceResolver implements NamespaceContext { //Store the source document to search the namespaces private Document sourceDocument; public NamespaceResolver(Document document) { sourceDocument = document; } //The lookup for the namespace uris is delegated to the stored document. public String getNamespaceURI(String prefix) { if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) { return sourceDocument.lookupNamespaceURI(null); } else { return sourceDocument.lookupNamespaceURI(prefix); } } public String getPrefix(String namespaceURI) { return sourceDocument.lookupPrefix(namespaceURI); } @SuppressWarnings("rawtypes") public Iterator getPrefixes(String namespaceURI) { return null; } } ``` ## 使用`NamespaceResolver`并應用 XPath 現在,我們準備在 XML 文件上應用 xpath 表達式。 ```java //Want to read all book names from XML ArrayList<String> bookNames = new ArrayList<String>(); //Parse XML file DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new FileInputStream(new File("sample.xml"))); //Get XPath expression XPathFactory xpathfactory = XPathFactory.newInstance(); XPath xpath = xpathfactory.newXPath(); xpath.setNamespaceContext(new NamespaceResolver(doc)); XPathExpression expr = xpath.compile("//ns2:bookStore/ns2:book/ns2:name/text()"); //Search XPath expression Object result = expr.evaluate(doc, XPathConstants.NODESET); //Iterate over results and fetch book names NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { bookNames.add(nodes.item(i).getNodeValue()); } //Verify book names System.out.println(bookNames); ``` 上面程序的輸出是: ```java [Data Structure, Java Core] ``` ## XPath 命名空間解析的完整源代碼 這是上面示例的完整源代碼。 ```java import java.io.File; import java.io.FileInputStream; import java.util.ArrayList; import java.util.Iterator; import javax.xml.XMLConstants; import javax.xml.namespace.NamespaceContext; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; public class Main { public static void main(String[] args) throws Exception { //Want to read all book names from XML ArrayList<String> bookNames = new ArrayList<String>(); //Parse XML file DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new FileInputStream(new File("sample.xml"))); //Get XPath expression XPathFactory xpathfactory = XPathFactory.newInstance(); XPath xpath = xpathfactory.newXPath(); xpath.setNamespaceContext(new NamespaceResolver(doc)); XPathExpression expr = xpath.compile("//ns2:bookStore/ns2:book/ns2:name/text()"); //Search XPath expression Object result = expr.evaluate(doc, XPathConstants.NODESET); //Iterate over results and fetch book names NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { bookNames.add(nodes.item(i).getNodeValue()); } //Verify book names System.out.println(bookNames); } } class NamespaceResolver implements NamespaceContext { //Store the source document to search the namespaces private Document sourceDocument; public NamespaceResolver(Document document) { sourceDocument = document; } //The lookup for the namespace uris is delegated to the stored document. public String getNamespaceURI(String prefix) { if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) { return sourceDocument.lookupNamespaceURI(null); } else { return sourceDocument.lookupNamespaceURI(prefix); } } public String getPrefix(String namespaceURI) { return sourceDocument.lookupPrefix(namespaceURI); } @SuppressWarnings("rawtypes") public Iterator getPrefixes(String namespaceURI) { return null; } } ``` 在評論部分讓我知道您的問題。 學習愉快! 參考: [http://www.ibm.com/developerworks/library/x-nmspccontext/](https://www.ibm.com/developerworks/library/x-nmspccontext/)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看