1

Is there a way to find a descendant element with JDOM, like you can with jQuery?

I'm trying to get the sequence element out of this XML document:

<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>

Currently I'm doing this:

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

But I'm thinking there must be a better way.

Thank you for your advice

Jon H
  • 193
  • 3
  • 15

1 Answers1

2

Use XPaths and read up on it here: JavaDoc for XPathFactory and some more about it here: GitHub Wiki

Bottom line is you want to have an XPath compiled with the expression "//xs:sequence" and the namespace Namespace.getNamespace("xs", "http://www.w3.org/2001/XMLSchema")

rolfl

rolfl
  • 17,539
  • 7
  • 42
  • 76
  • Thanks. I have replaced my code with ` Namespace.getNamespace("xs", "w3.org/2001/XMLSchema"); XPathExpression xpath = XPathFactory.instance().compile("//xs:sequence", Filters.element()); Element currentEl = xpath.evaluateFirst(document); ` But now I'm getting "Error 500: javax.servlet.ServletException: java.lang.IllegalArgumentException: Namespace with prefix 'xs' has not been declared." Any ideas? – Jon H May 02 '13 at 18:08
  • You have to include the xs namespace in the compile operation (and null for the variables map): XPathExpression xpath = XPathFactory.instance().compile("//xs:sequence", Filters.element(), null, xs); – rolfl May 02 '13 at 18:41
  • The namespace has to be declared for the context of the XPath expression, not just the XML document – rolfl May 02 '13 at 18:42