
Hi,
How to create dom tree from string data? I am trying to search the example for java dom from string.
Thanks

Hi,
Following code might be useful:
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
public class UpdateData
{
public static void main(String[] args)
{
String url="http://www.roseindia.net";
String strData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
strData = strData+"<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">\n";
strData = strData+" <url>\n";
strData = strData+" <loc><![CDATA[\"http://www.roseindia.net" + url +"\"]]></loc>\n";
strData = strData+" <changefreq>weekly</changefreq>\n";
strData = strData+" <priority>1.00</priority>\n";
strData = strData+" </url>\n";
strData = strData+"</urlset>\n";
System.out.println(strData);
System.out.println("hello");
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource( new StringReader( strData ) );
Document d = builder.parse( is );
}
catch( Exception ex ) {
ex.printStackTrace();
}
}
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.