मैंने पिछले दिन एक दस्तावेज़ को एक XML नोड निकालने का प्रयास किया है और इसे काम करने के लिए एक्सएमएल नेमस्पेस की बारीकियों को समझने में असमर्थ हूं।XPath, एक्सएमएल नेमस्पेस और जावा
एक्सएमएल फ़ाइल इसलिए यहाँ कुल में पोस्ट करने के लिए बड़ी है भाग है कि मुझे चिंता है:
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<XFDL xmlns="http://www.PureEdge.com/XFDL/6.5" xmlns:custom="http://www.PureEdge.com/XFDL/Custom" xmlns:designer="http://www.PureEdge.com/Designer/6.1" xmlns:pecs="http://www.PureEdge.com/PECustomerService" xmlns:xfdl="http://www.PureEdge.com/XFDL/6.5">
<globalpage sid="global">
<global sid="global">
<xmlmodel xmlns:xforms="http://www.w3.org/2003/xforms">
<instances>
<xforms:instance id="metadata">
<form_metadata>
<metadataver version="1.0"/>
<metadataverdate>
<date day="05" month="Jul" year="2005"/>
</metadataverdate>
<title>
<documentnbr number="2062" prefix.army="DA" scope="army" suffix=""/>
<longtitle>HAND RECEIPT/ANNEX NUMBER </longtitle>
</title>
दस्तावेज़ जारी है और अच्छी तरह से सभी तरह से नीचे ही बना है। मैं "documentnbr" टैग (नीचे से तीन) से "संख्या" विशेषता निकालने का प्रयास कर रहा हूं।
कोड है कि मैं यह करने के लिए उपयोग कर रहा हूँ इस तरह दिखता है:
/***
* Locates the Document Number information in the file and returns the form number.
* @return File's self-declared number.
* @throws InvalidFormException Thrown when XPath cannot find the "documentnbr" element in the file.
*/
public String getFormNumber() throws InvalidFormException
{
try{
XPath xPath = XPathFactory.newInstance().newXPath();
xPath.setNamespaceContext(new XFDLNamespaceContext());
Node result = (Node)xPath.evaluate(QUERY_FORM_NUMBER, doc, XPathConstants.NODE);
if(result != null) {
return result.getNodeValue();
} else {
throw new InvalidFormException("Unable to identify form.");
}
} catch (XPathExpressionException err) {
throw new InvalidFormException("Unable to find form number in file.");
}
}
कहाँ QUERY_FORM_NUMBER मेरी XPath अभिव्यक्ति है, और XFDLNamespaceContext NamespaceContext लागू करता है और इस तरह दिखता है:
public class XFDLNamespaceContext implements NamespaceContext {
@Override
public String getNamespaceURI(String prefix) {
if (prefix == null) throw new NullPointerException("Invalid Namespace Prefix");
else if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX))
return "http://www.PureEdge.com/XFDL/6.5";
else if ("custom".equals(prefix))
return "http://www.PureEdge.com/XFDL/Custom";
else if ("designer".equals(prefix))
return "http://www.PureEdge.com/Designer/6.1";
else if ("pecs".equals(prefix))
return "http://www.PureEdge.com/PECustomerService";
else if ("xfdl".equals(prefix))
return "http://www.PureEdge.com/XFDL/6.5";
else if ("xforms".equals(prefix))
return "http://www.w3.org/2003/xforms";
else
return XMLConstants.NULL_NS_URI;
}
@Override
public String getPrefix(String arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public Iterator getPrefixes(String arg0) {
// TODO Auto-generated method stub
return null;
}
}
मैं मैंने कई अलग-अलग XPath प्रश्नों का प्रयास किया है, लेकिन मुझे लगता है कि यह काम करना चाहिए:
protected static final String QUERY_FORM_NUMBER =
"/globalpage/global/xmlmodel/xforms:instances/instance" +
"/form_metadata/title/documentnbr[number]";
दुर्भाग्यवश यह काम नहीं करता है और मुझे लगातार शून्य वापसी मिलती है।
मैंने here, here, और here पढ़ने की उचित मात्रा में काम किया है, लेकिन कुछ भी काम करने में मेरी सहायता करने के लिए पर्याप्त रूप से रोशनी साबित हुई है।
मैं लगभग सकारात्मक हूं कि जब मैं इसे समझता हूं तो मैं चेहरे पर जा रहा हूं लेकिन मैं वास्तव में बुद्धिमान हूं कि मैं क्या खो रहा हूं।
इस सब के माध्यम से पढ़ने के लिए धन्यवाद और सहायता के लिए अग्रिम धन्यवाद।
-Andy
बिल्कुल सही समाधान है, मैं अभी भी काफी नामस्थान समझ में नहीं आता पर लेकिन मामले में आप स्पष्ट रूप से अतिरिक्त नामस्थान आप इस का उपयोग कर सकते परिभाषित करने की जरूरत कम से कम कोड अब काम करता है। बहुत धन्यवाद। – MrWizard54
@ जेसन: "मुझे कुछ कारणों से अभिव्यक्ति में किसी भी नामस्थान का उपयोग करने की ज़रूरत नहीं है।" मानक जावा कार्यान्वयन में, DocumentBuilderFactory डिफ़ॉल्ट रूप से नामस्थान-_unaware_ पार्सर्स का उत्पादन करता है। दस्तावेज़बिल्डर उत्पन्न करने से पहले 'dbfac.SetNamespaceAware (true) 'जोड़ना परिणाम बदल सकता है। –