【问题标题】:Find descendant element with JDOM使用 JDOM 查找后代元素
【发布时间】:2013-03-23 22:12:10
【问题描述】:

有没有办法使用 JDOM 找到后代元素,就像使用 jQuery 一样?

我正在尝试从这个 XML 文档中获取序列元素:

<getEmployeesXMLResponse xmlns="http://tempuri.org/">
    <getEmployeesXMLResult>
        <xs:schema id="NewDataSet" xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
            <xs:element name="NewDataSet" msdata:IsDataSet="true"
                msdata:MainDataTable="ListOfEmployees"
                msdata:UseCurrentLocale="true">
                <xs:complexType>
                    <xs:choice minOccurs="0" maxOccurs="unbounded">
                        <xs:element name="ListOfEmployees">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element name="name" type="xs:string" minOccurs="0" />
                                    <xs:element name="department" type="xs:string"
                                        minOccurs="0" />
                                </xs:sequence>
                            </xs:complexType>
                        </xs:element>
                    </xs:choice>
                </xs:complexType>
            </xs:element>
        </xs:schema>
        <diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"
            xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
            <DocumentElement xmlns="">
                <ListOfEmployees diffgr:id="ListOfEmployees1"
                    msdata:rowOrder="0">
                    <name>Paul</name>
                    <department>Sales</department>
                </ListOfEmployees>
                <ListOfEmployees diffgr:id="ListOfEmployees2"
                    msdata:rowOrder="1">
                    <name>Bob</name>
                    <department>Marketing</department>
                </ListOfEmployees>
            </DocumentElement>
        </diffgr:diffgram>
    </getEmployeesXMLResult>
</getEmployeesXMLResponse>

目前我正在这样做:

        Element currentEl = document.getRootElement();
        do {
            currentEl = currentEl.getChildren().get(0);
        } while (currentEl.getName() != "sequence");

但我认为一定有更好的方法。

感谢您的建议

【问题讨论】:

    标签: java xml jdom jdom-2


    【解决方案1】:

    使用 XPaths 并在此处阅读:JavaDoc for XPathFactory 并在此处了解更多信息:GitHub Wiki

    底线是您希望使用表达式“//xs:sequence”和命名空间 Namespace.getNamespace("xs", "http://www.w3.org/2001/XMLSchema") 编译 XPath

    罗尔夫

    【讨论】:

    • 谢谢。我已将我的代码替换为 `Namespace.getNamespace("xs", "w3.org/2001/XMLSchema"); XPathExpression xpath = XPathFactory.instance().compile("//xs:sequence", Filters.element());元素 currentEl = xpath.evaluateFirst(document); ` 但现在我收到“错误 500:javax.servlet.ServletException:java.lang.IllegalArgumentException:尚未声明前缀为 'xs' 的命名空间。”有什么想法吗?
    • 您必须在编译操作中包含 xs 命名空间(变量映射为 null): XPathExpression xpath = XPathFactory.instance().compile("//xs:sequence", Filters.element(), null, xs);
    • 必须为 XPath 表达式的上下文声明命名空间,而不仅仅是 XML 文档
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    • 2016-05-11
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 2019-07-25
    相关资源
    最近更新 更多