मैं एक एक्सएमएल फ़ाइल में पढ़ने के लिए जेडीओएम का उपयोग करना चाहता हूं, फिर जेडीओएम दस्तावेज़ से डेटा निकालने के लिए XPath का उपयोग करें। यह दस्तावेज़ ऑब्जेक्ट ठीक बनाता है, लेकिन जब मैं तत्वों की सूची के लिए दस्तावेज़ से पूछताछ करने के लिए XPath का उपयोग करता हूं, तो मुझे कुछ भी नहीं मिलता है।डिफ़ॉल्ट एक्सएमएल नेमस्पेस, जेडीओएम, और एक्सपीएथ
मेरा एक्सएमएल दस्तावेज़ मूल तत्व में परिभाषित एक डिफ़ॉल्ट नेमस्पेस है। मजाकिया बात यह है कि, जब मैं डिफ़ॉल्ट नामस्थान हटा देता हूं, तो यह सफलतापूर्वक XPath क्वेरी चलाता है और जो तत्व मैं चाहता हूं उसे वापस देता है। परिणामों को वापस करने के लिए मेरी XPath क्वेरी प्राप्त करने के लिए मुझे और क्या करना चाहिए?
एक्सएमएल:
<?xml version="1.0" encoding="UTF-8"?>
<collection xmlns="http://www.foo.com">
<dvd id="A">
<title>Lord of the Rings: The Fellowship of the Ring</title>
<length>178</length>
<actor>Ian Holm</actor>
<actor>Elijah Wood</actor>
<actor>Ian McKellen</actor>
</dvd>
<dvd id="B">
<title>The Matrix</title>
<length>136</length>
<actor>Keanu Reeves</actor>
<actor>Laurence Fishburne</actor>
</dvd>
</collection>
जावा:
public static void main(String args[]) throws Exception {
SAXBuilder builder = new SAXBuilder();
Document d = builder.build("xpath.xml");
XPath xpath = XPath.newInstance("collection/dvd");
xpath.addNamespace(d.getRootElement().getNamespace());
System.out.println(xpath.selectNodes(d));
}
चाल क्या थी, धन्यवाद! – Michael
यह उत्कृष्ट है, मैंने पिछले 3 घंटों में सोचा कि क्यों मेरा XPath अचानक काम नहीं कर रहा है और यह था। Pffh! :) – Esko
एक्स के लिए क्या है? – Meinkraft