<bdo id='9kEgl'></bdo><ul id='9kEgl'></ul>
    1. <small id='9kEgl'></small><noframes id='9kEgl'>

      1. <tfoot id='9kEgl'></tfoot>
        <i id='9kEgl'><tr id='9kEgl'><dt id='9kEgl'><q id='9kEgl'><span id='9kEgl'><b id='9kEgl'><form id='9kEgl'><ins id='9kEgl'></ins><ul id='9kEgl'></ul><sub id='9kEgl'></sub></form><legend id='9kEgl'></legend><bdo id='9kEgl'><pre id='9kEgl'><center id='9kEgl'></center></pre></bdo></b><th id='9kEgl'></th></span></q></dt></tr></i><div id='9kEgl'><tfoot id='9kEgl'></tfoot><dl id='9kEgl'><fieldset id='9kEgl'></fieldset></dl></div>

        <legend id='9kEgl'><style id='9kEgl'><dir id='9kEgl'><q id='9kEgl'></q></dir></style></legend>

        Java XPath:使用默认命名空间 xmlns 进行查询

        Java XPath: Queries with default namespace xmlns(Java XPath:使用默认命名空间 xmlns 进行查询)

                <tbody id='rV6VT'></tbody>
            1. <legend id='rV6VT'><style id='rV6VT'><dir id='rV6VT'><q id='rV6VT'></q></dir></style></legend>
              • <bdo id='rV6VT'></bdo><ul id='rV6VT'></ul>
                <i id='rV6VT'><tr id='rV6VT'><dt id='rV6VT'><q id='rV6VT'><span id='rV6VT'><b id='rV6VT'><form id='rV6VT'><ins id='rV6VT'></ins><ul id='rV6VT'></ul><sub id='rV6VT'></sub></form><legend id='rV6VT'></legend><bdo id='rV6VT'><pre id='rV6VT'><center id='rV6VT'></center></pre></bdo></b><th id='rV6VT'></th></span></q></dt></tr></i><div id='rV6VT'><tfoot id='rV6VT'></tfoot><dl id='rV6VT'><fieldset id='rV6VT'></fieldset></dl></div>

                <small id='rV6VT'></small><noframes id='rV6VT'>

              • <tfoot id='rV6VT'></tfoot>

                  本文介绍了Java XPath:使用默认命名空间 xmlns 进行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想对此文件进行 XPath 查询(显示摘录):

                  I want to do an XPath query on this file (excerpt shown):

                  <?xml version="1.0" encoding="UTF-8"?>
                  <!-- MetaDataAPI generated on: Friday, May 25, 2007 3:26:31 PM CEST -->
                  <ModelClass xmlns="http://xml.sap.com/2002/10/metamodel/webdynpro" xmlns:IDX="urn:sap.com:WebDynpro.ModelClass:2.0">
                      <ModelClass.Parent>
                          <Core.Reference package="com.test.mypackage" name="ModelName" type="Model"/>
                  

                  这是我正在使用的代码片段:

                  This is a snippet of the code I'm using:

                  DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
                  DocumentBuilder builder = domFactory.newDocumentBuilder();
                  Document document = builder.parse(new File(testFile));
                  XPathFactory factory = XPathFactory.newInstance();
                  XPath xpath = factory.newXPath();
                  xpath.setNamespaceContext( new NamespaceContext() {
                      public String getNamespaceURI(String prefix) {
                  ...
                  
                  String result = xpath.evaluate(xpathQueryString, document);
                  System.out.println(result);
                  

                  我面临的问题是,当在 XPath 查询中引用默认命名空间时,不会调用 getNamespaceURI 方法来解决它.例如,此查询不会提取任何内容:

                  The problem I'm facing is that when the default namespace is referenced in an XPath query, the getNamespaceURI method is not called to resolve it. This query for example doesn't extract anything:

                  //xmlns:ModelClass.Parent/xmlns:Core.Reference[@type="Model"]/@package

                  现在我尝试通过将 xmlns 替换为假前缀 d 来欺骗"解析器,然后相应地编写 getNamespaceURI 方法(所以在遇到 d 时返回 http://xml.sap.com/2002/10/metamodel/webdynpro).在这种情况下,会调用 getNamespaceURI,但 XPath 表达式求值的结果始终是空字符串.

                  Now I've tried "tricking" the parser by replacing xmlns with a fake prefix d and then writing the getNamespaceURI method accordingly (so to return http://xml.sap.com/2002/10/metamodel/webdynpro when d is encountered). In this case, the getNamespaceURI is called but the result of the XPath expression evaluation is always an empty string.

                  如果我从文件和 XPath 查询表达式中去除命名空间,我可以得到我想要的字符串 (com.test.mypackage).

                  If I strip out namespaces from the file and from the XPath query expression, I can get the string I wanted (com.test.mypackage).

                  有没有办法让默认命名空间正常工作?

                  Is there a way to make things work properly with the default namespace?

                  推荐答案

                  在您的 Namespace 上下文中,将您选择的前缀(例如 df)绑定到命名空间 URI在文档中

                  In your Namespace context, bind a prefix of your choice (e.g. df) to the namespace URI in the document

                  xpath.setNamespaceContext( new NamespaceContext() {
                      public String getNamespaceURI(String prefix) {
                        switch (prefix) {
                          case "df": return "http://xml.sap.com/2002/10/metamodel/webdynpro";
                          ...
                         }
                      });
                  

                  然后在路径表达式中使用该前缀来限定元素名称,例如/df:ModelClass/df:ModelClass.Parent/df:Core.Reference[@type = 'Model']/@package.

                  and then use that prefix in your path expressions to qualify element names e.g. /df:ModelClass/df:ModelClass.Parent/df:Core.Reference[@type = 'Model']/@package.

                  这篇关于Java XPath:使用默认命名空间 xmlns 进行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
                  Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
                  How to convert Integer to int?(如何将整数转换为整数?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
                  Inconsistent behavior on java#39;s ==(java的行为不一致==)
                  Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)

                  <small id='boNIE'></small><noframes id='boNIE'>

                  <legend id='boNIE'><style id='boNIE'><dir id='boNIE'><q id='boNIE'></q></dir></style></legend>
                • <i id='boNIE'><tr id='boNIE'><dt id='boNIE'><q id='boNIE'><span id='boNIE'><b id='boNIE'><form id='boNIE'><ins id='boNIE'></ins><ul id='boNIE'></ul><sub id='boNIE'></sub></form><legend id='boNIE'></legend><bdo id='boNIE'><pre id='boNIE'><center id='boNIE'></center></pre></bdo></b><th id='boNIE'></th></span></q></dt></tr></i><div id='boNIE'><tfoot id='boNIE'></tfoot><dl id='boNIE'><fieldset id='boNIE'></fieldset></dl></div>
                    <tbody id='boNIE'></tbody>

                        <tfoot id='boNIE'></tfoot>

                          <bdo id='boNIE'></bdo><ul id='boNIE'></ul>