Java dom from string

Ads

 
 

Share on Google+Share on Google+

Deepak Kumar
Java dom from string
1 Answer(s)      6 years and 6 months ago
Posted in : Java Beginners

Hi,

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

Thanks

Ads
View Answers

March 31, 2011 at 5:37 PM


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


Ads









Related Tutorials/Questions & Answers:
Java dom from string
Java dom from string  Hi, How to create dom tree from string data? I am trying to search the example for java dom from string. Thanks   ... UpdateData { public static void main(String[] args) { String url="http
dom to xml string
dom to xml string  Hi, I am writing a program in java that reads... have save the dom document into xml file. How to convert dom to xml string... for converting DOM object into string data. Please check the thread dom to xml
Advertisements
java code to create xml document from DOM object
java code to create xml document from DOM object  Hey! After knowing the way we can create DOM objects and add elements to it-> then displaying it on the console ;is there a way I can output the same in xml document
Removing HTML from a Java String
Removing HTML from a Java String  Removing HTML from a Java String
Dom in java
Dom in java  Exception in thread "main" org.w3c.dom.DOMException...(CoreDocumentImpl.java:618) at CreatXMLFile.main(CreatXMLFile.java:29)   Java Create XML using DOM import java.io.*; import org.w3c.dom.*; import
Java: Searching a string from doc files
Java: Searching a string from doc files   I want to search a string from DOC, DOCX files in java. Can anyone please help me
how to write to file from string in Java
how to write to file from string in Java  Hi, Please any one help me for how to write to file from string in Java. I am beginner in Java... string in java? So, then visit this http://www.roseindia.net/java/examples/io
Java-XML-DOM - XML
Java-XML-DOM  Hi! I need some help. I have to make java program that loads an xml file and from it builds DOM(later i will have to work with it - like using xpath in java find some value and replace it...). Since i'm new to java
Pass a dom object from jsp to servlet
Pass a dom object from jsp to servlet  I am creating a dom object in my jsp page. now i want to pass that object in a servlet through calling servlet in jsp. can anyone help me
DOM API - Java Beginners
DOM API  How can i go through each and every node of an xml file Each... main(String[] args) { try{ BufferedReader buff = new BufferedReader...: "); String str = buff.readLine(); File file = new File(str
DOM API - Java Beginners
DOM API  How can i go through each and every node of an xml file Each and everyChild? my xml file is given below: 810 32 141 22
Java Write To File From String
Java Write To File From String In this tutorial you will learn how to write to file from string. Write to file from string using java at first we will have... main(String args[]) { String str = "This example demonstrates
Java Remove a character from string
Java Remove a character from string In this tutorial, you will learn how to remove a character from the string. There are several methods for examining... to remove a particular character from the string. In the given example, we have
Java get Int from String
Java get Int from String     ... from String. For this, we have defined a string. The method  Integer.parseInt(st) converts the string into the integer. Here is the code
XML DOM error - Java Beginners
XML DOM error  import org.w3c.dom.*; import javax.xml.parsers....(String[] args) { try { BufferedReader bf = new BufferedReader(new...: "); String xmlFile = bf.readLine(); File file = new File(xmlFile
DOM
DOM  hello, What is DOM?   hii, DOM is stands for Document object Model. DOM is an interface-oriented Application Programming Interface. We can use it for navigation of the entire document
Find Longest Word from the string using Java
Find Longest Word from the string using Java Here we are going to find the longest word from the string. For this, we have specified the string which is then splitted into string array using  split() method. Then we have created
DOM
DOM  package name for Document class?   Hi Friend, The package is known as dom api. For more information, visit the following links: http://www.roseindia.net/xml/ http://www.roseindia.net/xml/dom/ Thanks
Extarct string from PDF
Extarct string from PDF  how can i search a particular character from pdf? also extarct string from a pdf document
Removing characters from a string
Removing characters from a string  Removing characters from a string...(String []args){ String str="Hello 123.you are At 3456"; String...() method removes all blank spaces in the string
write a java program for inserting a substring in to the given main string from a given position
write a java program for inserting a substring in to the given main string from a given position  write a java program for inserting a substring in to the given main string from a given position
Java DOM Tutorial
Java DOM Tutorial      ... element from the XML document.  The JAXP (Java APIs for XML...; Accessing XML file from Java In this example we have provided you a simple java
Remove Repeated Characters from the String
Remove Repeated Characters from the String In this we are going to remove repeated characters from the string. For this, we have allowed the user to enter the string. The class BufferedReader reads the string from the console. Then we
remove a substring from a string
remove a substring from a string  Please I jave an arraylist composed as follwoing [w1, w2, w3, w4, w1.w2.w3, w2.w3.w4, w3.w4, w2.w4, w1.w3, w1.w2] w2.w4 is a subset of w2.w3.w4 ?how I recognize it also w1.w3 is a subset
remove comma from string php
remove comma from string php  How to remove the last comma from the string in PHP
Removing character from a string in sql.
Removing character from a string in sql.  How to remove or replace character from a string
Removing character from a string in sql.
Removing character from a string in sql.  Removing special characters from a string in sql
How to return a string from a JTextField
How to return a string from a JTextField  Hi, Am new to java, and am trying to get a string from a JTextField so that I can compare it with another String using either compare or equals or compareTo methods of the String class
Java string
Java string  Strings are immutable.But String s="Hello"; String s1=s+"World"; S.O.P(s1); means printing "Hello World". How
remove comma from string php
remove comma from string php  How to remove the last comma from the string in PHP?   remove last comma from string names = names.replaceAll(",$", ""); //Or a simple substring: if (names.endsWith(",")) { names
Stripping of unwanted characters from string
Stripping of unwanted characters from string  how to strip off unwanted characters from a string   Hi Friend, Try this: public class StripCharacters { public String strip(String s) { String st

Ads

 
Advertisement null

Ads