इस सवाल का पहले से ही एक उत्तर है: < / P>
- 4 जवाब
सबसे तेज़ तरीका क्या है जावा में एक विशाल एक्सएमएल फ़ाइल पूछें,
DOM - xpath: यह बहुत समय ले रहा है,
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance (); docBuilderFactory.setNamespaceAware (सही); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder (); दस्तावेज़ दस्तावेज़ = docBuilder.parse (नई फ़ाइल ("test.xml")); XPath xpath = XPathFactory.newInstance ()। NewXPath (); स्ट्रिंग xPath = "/ * / * [@ id = 'ABCD'] / * / *"; XPathExpression expr = xpath.compile (xPath); // इस लाइन में बहुत समय लगता है नोडलिस्ट परिणाम = (नोडलिस्ट) expr.evaluate (दस्तावेज़, XPathConstants.NODESET);कोड में अंतिम पंक्ति के साथ, कार्यक्रम 40 सेकंड में समाप्त होता है और इसके बिना 1 सेकंड में।
SAX: मुझे नहीं पता कि यह क्वेरी के लिए उपयोग किया जा सकता है , इंटरनेट पर मैं केवल पार्सिंग के उदाहरण ढूंढने में सक्षम हूं।
क्वेरी को तेज़ी से करने के लिए अन्य विकल्प क्या हैं, मेरी एक्सएमएल फ़ाइल का आकार लगभग 5 एमबी है Thnx
यदि आपका
id विशेषताएंxs: आईडी और आपके पास अपने दस्तावेज़ के लिए एक XML स्कीमा है, तो आपDocument.getElementById (स्ट्रिंग) विधि का उपयोग कर सकते हैं। मैं एक उदाहरण के साथ नीचे प्रदर्शित होगा।एक्सएमएल स्कीमा
& lt;? Xml संस्करण = "1.0" एन्कोडिंग = "यूटीएफ -8" ? & gt; & Lt; स्कीमा xmlns = "http://www.w3.org/2001/XMLSchema" targetNamespace = "http://www.example.org/schema" xmlns: tns = "http://www.example.org/ स्कीमा "elementFormDefault =" qualified "& gt; & Lt; element name = "foo" & gt; & LT; complexType & gt; & LT; अनुक्रम & gt; & Lt; element ref = "tns: बार" maxOccurs = "unbounded" / & gt; & Lt; / अनुक्रम & gt; & Lt; / complexType & gt; & Lt; / तत्व & gt; & Lt; element name = "bar" & gt; & LT; complexType & gt; & Lt; एट्रिब्यूट नाम = "आईडी" प्रकार = "आईडी" / & gt; & Lt; / complexType & gt; & Lt; / तत्व & gt; & Lt; / स्कीमा & gt;XML इनपुट (input.xml)
& lt;? Xml संस्करण = "1.0" एन्कोडिंग = "UTF-8 "& gt; & Lt; foo xmlns = "http://www.example.org/schema" & gt; & Lt; bar id = "ABCD" / & gt; & Lt; bar id = "EFGH" / & gt; & Lt; bar id = "IJK" / & gt; & Lt; / foo & gt;डेमो
आपको
स्कीमा काDocumentBuilderFactory पर उदाहरण सेट करना होगा सब कुछ काम करने के लिए।import java.io.File; आयात javax.xml.XMLConstants; आयात javax.xml.parsers। *; आयात javax.xml.validation। *; आयात org.w3c.dom। *; पब्लिक क्लास डेमो {सार्वजनिक स्थिर शून्य मुख्य (स्ट्रिंग [] एजीआर) अपवाद फेंकता है {स्कीमा फैक्टरिंग एसएफ = स्कीमाफैक्चर। न्यू ईन्स्टेंस (XMLConstants.W3C_XML_SCHEMA_NS_URI); स्कीमा स्कीमा = sf.newSchema (नई फ़ाइल ("src / forum17250259 / schema.xsd")); DocumentBuilderFactory डीबीएफ = DocumentBuilderFactory.newInstance (); dbf.setNamespaceAware (सही); dbf.setSchema (स्कीमा); DocumentBuilder db = dbf.newDocumentBuilder (); दस्तावेज़ दस्तावेज़ = db.parse (नया फ़ाइल ("src / forum17250259 / input.xml")); तत्व परिणाम = दस्तावेज़.getElementById ("EFGH"); Println (परिणाम); }}
No comments:
Post a Comment