1
有沒有辦法使用JDOM查找後代元素,就像使用jQuery一樣?使用JDOM查找後代元素
我試圖讓序列元素這份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");
但我想一定有一個更好的辦法。
謝謝您的建議
感謝。我用' 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 ); –
rolfl
必須爲XPath表達式的上下文聲明名稱空間,而不僅僅是XML文檔 – rolfl