Sometimes you will begin with your XML in String form and want to be able to parse the DOM. You need a Document. Here's one way to do it. The code is HERE:
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Scanner;
import javax.xml.parsers.DocumentBuilderFactory;
public class XmlGenerator {
public static void main(String[] args) throws Exception {
String xml = "ROOT(PARAMS,PARAM1,PARAM2,PARAM3)";
Scanner s = new Scanner(xml);
s.useDelimiter("[(),\\s]");
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
.newDocument();
Node root = doc.createElement(s.next());
doc.appendChild(root);
Node sheet = doc.createElement(s.next());
root.appendChild(sheet);
while (s.hasNext()) {
Node param = doc.createElement(s.next());
sheet.appendChild(param);
}
System.out.println(XmlGenerator.documentToString(doc));
}
/**
* Serialize the DOM to String. Using the writeToString method
* of LSSerializer I found inserted the wrong encoding.
*
* @param doc The Document to serialize to String
*/
public static String documentToString(Document doc) {
Writer out = null;
String result = null;
try {
DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
LSSerializer lsSerializer = domImplementation.createLSSerializer();
lsSerializer.getDomConfig()
.setParameter("format-pretty-print", Boolean.TRUE);
LSOutput output = domImplementation.createLSOutput();
output.setEncoding(System.getProperty("file.encoding"));
out = new StringWriter();
output.setCharacterStream(out);
lsSerializer.write(doc, output);
result = out.toString();
} finally {
if (out != null) {
}
}
return result;
}
}
import org.w3c.dom.Node;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Scanner;
import javax.xml.parsers.DocumentBuilderFactory;
public class XmlGenerator {
public static void main(String[] args) throws Exception {
String xml = "ROOT(PARAMS,PARAM1,PARAM2,PARAM3)";
Scanner s = new Scanner(xml);
s.useDelimiter("[(),\\s]");
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
.newDocument();
Node root = doc.createElement(s.next());
doc.appendChild(root);
Node sheet = doc.createElement(s.next());
root.appendChild(sheet);
while (s.hasNext()) {
Node param = doc.createElement(s.next());
sheet.appendChild(param);
}
System.out.println(XmlGenerator.documentToString(doc));
}
/**
* Serialize the DOM to String. Using the writeToString method
* of LSSerializer I found inserted the wrong encoding.
*
* @param doc The Document to serialize to String
*/
public static String documentToString(Document doc) {
Writer out = null;
String result = null;
try {
DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
LSSerializer lsSerializer = domImplementation.createLSSerializer();
lsSerializer.getDomConfig()
.setParameter("format-pretty-print", Boolean.TRUE);
LSOutput output = domImplementation.createLSOutput();
output.setEncoding(System.getProperty("file.encoding"));
out = new StringWriter();
output.setCharacterStream(out);
lsSerializer.write(doc, output);
result = out.toString();
} finally {
if (out != null) {
}
}
return result;
}
}