hientx

Just another WordPress.com site

Category Archives: Java

convert xml file to string xml, convert string xml to document and convert string to InputStream

import java.io.ByteArrayInputStream;

04.import java.io.InputStream;
05.import java.io.UnsupportedEncodingException;
07.public class StringToStream {
08.    public static void main(String[] args) {
09.        String text = "Converting String to InputStream Example";
16.        try {
17.            InputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
18.        } catch (UnsupportedEncodingException e) {
19.            e.printStackTrace();
20.        }
21.    }
22.}

Convert XML file to XML String in java

I had to find out a way to read an XML file, and convert it to java String. Below is the code snippet I used.

public String convertXMLFileToString(String fileName)
{
try{
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
InputStream inputStream = new FileInputStream(new File(fileName));
org.w3c.dom.Document doc = documentBuilderFactory.newDocumentBuilder().parse(inputStream);
StringWriter stw = new StringWriter();
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.transform(new DOMSource(doc), new StreamResult(stw));
return stw.toString();
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}

Convert String XML to XML Document in java

Example 1: Element node =  DocumentBuilderFactory     .newInstance()     .newDocumentBuilder()     .parse(new StringInputStream("<node>value</node>"))     .getDocumentElement(); Example 2: 
/**    * @param docBuilder    *          the parser    * @param parent    *          node to add fragment to    * @param fragment    *          a well formed XML fragment    */   public static void appendXmlFragment(       DocumentBuilder docBuilder, Node parent,       String fragment) throws IOException, SAXException {     Document doc = parent.getOwnerDocument();     Node fragmentNode = docBuilder.parse(         new InputSource(new StringReader(fragment)))         .getDocumentElement();     fragmentNode = doc.importNode(fragmentNode, true);     parent.appendChild(fragmentNode);   } 
Example 3: 

Then get the Document into which the new node is inserted, and the parent Element (to be) from it. (Your org.w3c.dom.Document would need to be converted to org.dom4j.Document here.) For testing purposes, I created one like this:

    Document originalDoc =       new SAXReader().read(new StringReader("<root><given></given></root>"));     Element givenNode = originalDoc.getRootElement().element("given"); 

Adding the new child element is very simple:

    givenNode.add(newNodeDocument.getRootElement()); 

Done. Outputting originalDoc now yields:

<?xml version="1.0" encoding="utf-8"?> <root>     <given>         <node>value</node>     </given> </root> 

Appendix: Because your question talks about org.w3c.dom.Document, here’s how to convert between that and org.dom4j.Document.

// dom4j -> w3c DOMWriter writer = new DOMWriter(); org.w3c.dom.Document w3cDoc = writer.write(dom4jDoc); // w3c -> dom4j DOMReader reader = new DOMReader(); Document dom4jDoc = reader.read(w3cDoc); 
 Source: http://stackoverflow.com/questions/729621/convert-string-xml-fragment-to-document-node-in-java
 http://sacrosanctblood.blogspot.com/2008/07/convert-xml-file-to-xml-string-in-java.html 

>Ebook: .NET, C#, Java, PHP, SQLite, hacking, JQuery, CSS, HTML5,…

>Sẽ liên tục cập nhật các kho ebook cho anh em:
Kinh nghiệm of tôi là gõ: “mediafire.com” + “Tên Sách” trên google 😀

SQLite, CSS, HTML5, OpenGL
http://www.heronote.com/files/jQuery.mobile.htm

Ajax:
http://www.fshare.vn/file/2Q52GX00/
References: http://www.sotayweb.com/lap-trinh-thiet-ke-website/pro/lap-trinh-javascript/item/152-giao-trinh-tai-lieu-sach-ebook-huong-dan-hoc-ajax-co-ban-tieng-viet.html

http://www.elakiri.com/forum/showthread.php?p=8160091
http://www.mediafire.com/?sharekey=77bcd55cf9dfe16719747bd91027d4ddedc3d66572667f86