2011-04-26 7 views
6

का उपयोग कर XML दस्तावेज़ बनाएं, मुझे नोडलिस्ट का उपयोग करके XML दस्तावेज़ ऑब्जेक्ट बनाने की आवश्यकता है। क्या कोई मुझे ऐसा करने में मदद कर सकता है। मैं तुम्हें कोड और नीचेनोडलिस्ट

import 
javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.xpath.*; import 
org.w3c.dom.*; 

public class ReadFile { 

    public static void main(String[] args) { 
     String exp = "/configs/markets"; 
     String path = "testConfig.xml"; 
     try { 
      Document xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(path); 
      XPath xPath = XPathFactory.newInstance().newXPath(); 
      XPathExpression xPathExpression = xPath.compile(exp); 
      NodeList nodes = (NodeList) 
       xPathExpression.evaluate(xmlDocument, 
             XPathConstants.NODESET); 

     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 
} 

xml फ़ाइल एक्सएमएल से पता चला है पहले से नीचे

<configs> 
    <markets> 
     <market> 
      <name>Real</name> 
     </market> 
     <market> 
      <name>play</name> 
     </market> 
    </markets> 
</configs> 

धन्यवाद दिखाया गया है ..

उत्तर

12

आप इस तरह यह करना चाहिए:

  • आप एक बनाने के नई org.w3c.dom.Document newXmlDoc जहाँ आप अपने NodeList में नोड्स की दुकान,
  • आप एक नया मूल तत्व बनाते हैं, और newXmlDoc
  • को यह तो संलग्न, प्रत्येक नोड 0,123,669 के लिए

    public static void main(String[] args) { 
        String exp = "/configs/markets/market"; 
        String path = "src/a/testConfig.xml"; 
        try { 
         Document xmlDocument = DocumentBuilderFactory.newInstance() 
           .newDocumentBuilder().parse(path); 
    
         XPath xPath = XPathFactory.newInstance().newXPath(); 
         XPathExpression xPathExpression = xPath.compile(exp); 
         NodeList nodes = (NodeList) xPathExpression. 
           evaluate(xmlDocument, XPathConstants.NODESET); 
    
         Document newXmlDocument = DocumentBuilderFactory.newInstance() 
           .newDocumentBuilder().newDocument(); 
         Element root = newXmlDocument.createElement("root"); 
         newXmlDocument.appendChild(root); 
         for (int i = 0; i < nodes.getLength(); i++) { 
          Node node = nodes.item(i); 
          Node copyNode = newXmlDocument.importNode(node, true); 
          root.appendChild(copyNode); 
         } 
    
         printTree(newXmlDocument); 
        } catch (Exception ex) { 
         ex.printStackTrace(); 
        } 
    } 
    
    public static void printXmlDocument(Document document) { 
        DOMImplementationLS domImplementationLS = 
         (DOMImplementationLS) document.getImplementation(); 
        LSSerializer lsSerializer = 
         domImplementationLS.createLSSerializer(); 
        String string = lsSerializer.writeToString(document); 
        System.out.println(string); 
    } 
    

    उत्पादन होता है:

    <?xml version="1.0" encoding="UTF-16"?> 
    <root><market> 
          <name>Real</name> 
         </market><market> 
          <name>play</name> 
         </market></root> 
    

    अपने NodeList में, आप newXmlDoc में n आयात करते हैं, और फिर आप root

यहाँ के एक बच्चे के रूप n संलग्न कोड है कुछ नोट्स:

  • मैं /configs/markets/market को exp बदल दिया है, क्योंकि मैं तुम्हें नहीं बल्कि एक markets तत्व
  • printXmlDocument के लिए
  • से, market तत्वों की प्रतिलिपि बनाना चाहते संदेह है, मैं इस answer

में दिलचस्प कोड का उपयोग किया है आशा है कि ये आपकी मदद करेगा।


आप एक नया मूल तत्व बनाने के लिए नहीं करना चाहते हैं, तो आप अपने मूल XPath अभिव्यक्ति है, जो एक NodeList एक एकल नोड से मिलकर रिटर्न का उपयोग कर सकते (ध्यान रखें कि आपके XML एकल रूट होना आवश्यक है तत्व) कि आप सीधे अपने नए एक्सएमएल दस्तावेज़ में जोड़ सकते हैं।

public static void main(String[] args) { 
    //String exp = "/configs/markets/market/"; 
    String exp = "/configs/markets"; 
    String path = "src/a/testConfig.xml"; 
    try { 
     Document xmlDocument = DocumentBuilderFactory.newInstance() 
       .newDocumentBuilder().parse(path); 

     XPath xPath = XPathFactory.newInstance().newXPath(); 
     XPathExpression xPathExpression = xPath.compile(exp); 
     NodeList nodes = (NodeList) xPathExpression. 
     evaluate(xmlDocument,XPathConstants.NODESET); 

     Document newXmlDocument = DocumentBuilderFactory.newInstance() 
       .newDocumentBuilder().newDocument(); 
     //Element root = newXmlDocument.createElement("root"); 
     //newXmlDocument.appendChild(root); 
     for (int i = 0; i < nodes.getLength(); i++) { 
      Node node = nodes.item(i); 
      Node copyNode = newXmlDocument.importNode(node, true); 
      newXmlDocument.appendChild(copyNode); 
      //root.appendChild(copyNode); 
     } 

     printXmlDocument(newXmlDocument); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
} 

यह आप निम्नलिखित उत्पादन दे देंगे:

<?xml version="1.0" encoding="UTF-16"?> 
<markets> 
     <market> 
      <name>Real</name> 
     </market> 
     <market> 
      <name>play</name> 
     </market> 
    </markets> 
+0

यह ठीक काम करता है मार्को जहां मैं ऊपर कोड से वे पंक्तियां टिप्पणी की

, निम्नलिखित कोड देखें।लेकिन समस्या यह है कि रूट नामक तत्व है जो xml दस्तावेज़ में मूल रूप से नहीं है। रूट तत्व के बिना इसे करने का कोई तरीका है। अग्रिम धन्यवाद – nath

+0

अच्छी तरह से, आपको एक XML में रूट तत्व की आवश्यकता है। आप जो कर सकते हैं वह आपके मूल XPath ('स्ट्रिंग एक्स ="/कॉन्फ़िगर/मार्केट्स "; ') के साथ' मार्केट 'निकालें, और फिर आपके' नोडलिस्ट 'में एक एकल नोड होगा, जो सीधे आपके नए एक्सएमएल में आयात और संलग्न हो सकता है दस्तावेज़: संपादित उत्तर देखें। – MarcoS

+0

धन्यवाद मार्को। अब यह ठीक काम करता है .. :) – nath

0

आप Document की adoptNode() विधि का प्रयास कर सकते हैं।

शायद आपको अपने NodeList पर पुन: प्रयास करने की आवश्यकता होगी। आप nodeList.item(i) के साथ व्यक्तिगत Nodes तक पहुंच सकते हैं।

आप एक Element में अपने खोज परिणामों को रैप करने के लिए चाहते हैं, आप Document और appendChild() पर नव निर्मित से createElement() का उपयोग कर सकते Element

संबंधित मुद्दे