2009-05-14 13 views
21

मैं xstream में उपयोग किए जाने वाले XML दस्तावेज़ या नोड ऑब्जेक्ट से इनपुट इनपुट स्ट्रीम कैसे बना सकता हूं? मुझे बदलने की जरूरत है ??? कुछ सार्थक कोड के साथ। धन्यवाद।किसी दस्तावेज़ या नोड से इनपुटस्ट्रीम कैसे बनाएं

Document doc = getDocument(); 
InputStream is = ???; 
MyObject obj = (MyObject) xstream.fromXML(is); 

उत्तर

40
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
Source xmlSource = new DOMSource(doc); 
Result outputTarget = new StreamResult(outputStream); 
TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget); 
InputStream is = new ByteArrayInputStream(outputStream.toByteArray()); 
+1

यह बहुत अच्छा काम करता है, बहुत धन्यवाद। –

+0

आपने अंतिम पंक्ति में कोड की पहली पंक्ति का उपयोग किया था। यदि आप इसे जांचते हैं तो मध्य रेखाएं कुछ भी नहीं करतीं ... – AbhishekB

+2

पहली पंक्ति ByteArrayOUTPUTStream का उपयोग करती है जबकि अंतिम बार ByteArrayINPUTStream का उपयोग किया जाता है। फ़्यूथरमोर, पहली पंक्ति में घोषित आउटपुटस्ट्रीम को स्ट्रीमआरसल्ट के लिए एक पैरा के रूप में उपयोग किया जाता है। –

2

एक तरह से यह करने के लिए: DOMSource के साथ एक Source को Document अनुकूल बनाएं। ByteArrayOutputStream को अनुकूलित करने के लिए StreamResult बनाएं। डेटा भरने के लिए TransformerFactory.newTransformer से Transformer का उपयोग करें। अपने byte[] और ByteArrayInputStream के साथ स्ट्रीम पुनर्प्राप्त करें।

कोड को एक साथ रखना एक अभ्यास के रूप में छोड़ दिया गया है।

5

आप किसी भी तीसरे पक्ष के पुस्तकालय के बिना जावा का उपयोग कर रहे हैं, तो आप नीचे दिए गए कोड का उपयोग कर InputStream बना सकते हैं:

/* 
* Convert a w3c dom node to a InputStream 
*/ 
private InputStream nodeToInputStream(Node node) throws TransformerException { 
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
    Result outputTarget = new StreamResult(outputStream); 
    Transformer t = TransformerFactory.newInstance().newTransformer(); 
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 
    t.transform(new DOMSource(node), outputTarget); 
    return new ByteArrayInputStream(outputStream.toByteArray()); 
} 
2
public static InputStream document2InputStream(Document document) throws IOException { 
     ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
     OutputFormat outputFormat = new OutputFormat(document); 
     XMLSerializer serializer = new XMLSerializer(outputStream, outputFormat); 
     serializer.serialize(document); 
     return new ByteArrayInputStream(outputStream.toByteArray()); 
} 

यह काम करता है अगर तुम अपाचे Xerces कार्यान्वयन का उपयोग कर रहे हैं, आप भी सेट कर सकते हैं आउटपुट प्रारूप के साथ प्रारूप पैरामीटर।

1
public static InputStream documentToPrettyInputStream(Document document) throws IOException{ 

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
    XMLWriter xmlWriter = new XMLWriter(outputStream, OutputFormat.createPrettyPrint()); 
    xmlWriter.write(document); 
    xmlWriter.close(); 
    InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); 

    return inputStream; 

}

आप DOM4j उपयोग करने के लिए होता है और आप इसे सुंदर मुद्रित करने के लिए की जरूरत है!

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