无法使用 JDOM 提取 xml 值元素X路径

发布于 2024-12-02 13:33:46 字数 3133 浏览 3 评论 0原文

我有一个方法(getSingleNodeValue()),当传递 xpatch 表达式时,它将提取“doc”中引用的 xml 文档中指定元素的值。假设此时 doc 已初始化,如下所示,并且 xmlInput 是包含 xml 内容的缓冲区。

SAXBuilder  builder     =   null;
Document    doc     =   null; 
XPath       xpathInstance   =   null;

doc = builder.build(new StringReader(xmlInput));

当我调用该方法时,我传递以下 xpath xpression

/TOP4A/PERLODSUMDEC/TINPLD1/text()

这是该方法。它基本上只需要一个 xml 缓冲区并使用 xpath 来提取值:

public static String getSingleNodeValue(String xpathExpr) throws Exception{

    Text list = null;

    try {
        xpathInstance = XPath.newInstance(xpathExpr);
        list = (Text) xpathInstance.selectSingleNode(doc);
    } catch (JDOMException e) {
        throw new Exception(e);
    }catch (Exception e){
        throw new Exception(e);
    } 

    return list==null ? "?" : list.getText();
}

上面的方法总是返回“?”即没有找到任何内容,因此“列表”为空。 它查看的 xml 文档

<TOP4A xmlns="http://www.testurl.co.uk/enment/gqr/3232/1">    
  <HEAD>
    <Doc>ABCDUK1234</Doc>  
  </HEAD>    
  <PERLODSUMDEC>
    <TINPLD1>10109000000000000</TINPLD1>
  </PERLODSUMDEC>
</TOP4A>

与其他 xml 文档的方法相同,所以我不确定这个文档有什么特别之处。没有例外,因此 xml 是有效的 xml。只是该方法总是将“list”设置为 null。有什么想法吗?

编辑

好的,按照建议,这是一个简单的运行程序,演示了上面的内容

import org.jdom.*;
import org.jdom.input.*;
import org.jdom.xpath.*;

import java.io.IOException;
import java.io.StringReader;

public class XpathTest {


    public static String getSingleNodeValue(String xpathExpr, String xmlInput) throws Exception{

        Text list = null;
        SAXBuilder  builder         =   null;
        Document    doc             =   null; 
        XPath       xpathInstance   =   null;

        try {
            builder = new SAXBuilder(); 
            doc     = builder.build(new StringReader(xmlInput));

            xpathInstance = XPath.newInstance(xpathExpr);
            list = (Text) xpathInstance.selectSingleNode(doc);
        } catch (JDOMException e) {
            throw new Exception(e);
        }catch (Exception e){
            throw new Exception(e);
        } 

        return list==null ? "Nothing Found" : list.getText();
    }

    public static void main(String[] args){

        String xmlInput1 = "<TOP4A xmlns=\"http://www.testurl.co.uk/enment/gqr/3232/1\"><HEAD><Doc>ABCDUK1234</Doc></HEAD><PERLODSUMDEC><TINPLD1>10109000000000000</TINPLD1></PERLODSUMDEC></TOP4A>";
        String xpathExpr = "/TOP4A/PERLODSUMDEC/TINPLD1/text()";

        XpathTest xp = new XpathTest();
        try {
            System.out.println(xp.getSingleNodeValue(xpathExpr, xmlInput1));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

当我运行上面的程序时,输出是

Nothing found

编辑

我已经运行了一些进一步的测试,看来如果我删除命名空间 url,它确实可以工作。还不知道为什么。有什么方法可以告诉它忽略名称空间吗?

编辑

另请注意,上述内容是在 JDK1.4.1 上实现的,因此我没有更高版本 JDK 的选项。这就是我必须坚持使用 Jdom 的原因。

I have a method (getSingleNodeValue()) which when passed an xpatch expression will extract the value of the specified element in the xml document refered to in 'doc'. Assume doc at this point has been initialised as shown below and xmlInput is the buffer containing the xml content.

SAXBuilder  builder     =   null;
Document    doc     =   null; 
XPath       xpathInstance   =   null;

doc = builder.build(new StringReader(xmlInput));

When i call the method, i pass the following xpath xpression

/TOP4A/PERLODSUMDEC/TINPLD1/text()

Here is the method. It basically just takes an xml buffer and uses xpath to extract the value:

public static String getSingleNodeValue(String xpathExpr) throws Exception{

    Text list = null;

    try {
        xpathInstance = XPath.newInstance(xpathExpr);
        list = (Text) xpathInstance.selectSingleNode(doc);
    } catch (JDOMException e) {
        throw new Exception(e);
    }catch (Exception e){
        throw new Exception(e);
    } 

    return list==null ? "?" : list.getText();
}

The above method always returns "?" i.e. nothing is found so 'list' is null.
The xml document it looks at is

<TOP4A xmlns="http://www.testurl.co.uk/enment/gqr/3232/1">    
  <HEAD>
    <Doc>ABCDUK1234</Doc>  
  </HEAD>    
  <PERLODSUMDEC>
    <TINPLD1>10109000000000000</TINPLD1>
  </PERLODSUMDEC>
</TOP4A>

The same method works with other xml documents so i am not sure what is special about this one. There is no exception so the xml is valid xml. Its just that the method always sets 'list' to null. Any ideas?

Edit

Ok as suggested, here is a simple running program that demonstrates the above

import org.jdom.*;
import org.jdom.input.*;
import org.jdom.xpath.*;

import java.io.IOException;
import java.io.StringReader;

public class XpathTest {


    public static String getSingleNodeValue(String xpathExpr, String xmlInput) throws Exception{

        Text list = null;
        SAXBuilder  builder         =   null;
        Document    doc             =   null; 
        XPath       xpathInstance   =   null;

        try {
            builder = new SAXBuilder(); 
            doc     = builder.build(new StringReader(xmlInput));

            xpathInstance = XPath.newInstance(xpathExpr);
            list = (Text) xpathInstance.selectSingleNode(doc);
        } catch (JDOMException e) {
            throw new Exception(e);
        }catch (Exception e){
            throw new Exception(e);
        } 

        return list==null ? "Nothing Found" : list.getText();
    }

    public static void main(String[] args){

        String xmlInput1 = "<TOP4A xmlns=\"http://www.testurl.co.uk/enment/gqr/3232/1\"><HEAD><Doc>ABCDUK1234</Doc></HEAD><PERLODSUMDEC><TINPLD1>10109000000000000</TINPLD1></PERLODSUMDEC></TOP4A>";
        String xpathExpr = "/TOP4A/PERLODSUMDEC/TINPLD1/text()";

        XpathTest xp = new XpathTest();
        try {
            System.out.println(xp.getSingleNodeValue(xpathExpr, xmlInput1));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

When i run the above, the output is

Nothing found

Edit

I have run some further testing and it appears that if i remove the namespace url it does work. Not sure why yet. Is there any way i can tell it to ignore the namespace?

Edit

Please also note that the above is implemented on JDK1.4.1 so i dont have the options for later version of the JDKs. This is the reason why i had to stick with Jdom.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2024-12-09 13:33:46

问题在于 XML 命名空间:您的 XPath 查询首先在默认命名空间中选择“TOP4A”元素。但是,您的 XML 文件在“http://www.testurl.co.uk/enment/gqr/3232/1”命名空间中具有“TOP4A”元素。

是否可以选择从 XML 中删除 xmlns?

The problem is with XML namespaces: your XPath query starts by selecting a 'TOP4A' element in the default namespace. Your XML file, however, has a 'TOP4A' element in the 'http://www.testurl.co.uk/enment/gqr/3232/1' namespace instead.

Is it an option to remove the xmlns from the XML?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文