Board index » jbuilder » XML output - how to turn off declaration?

XML output - how to turn off declaration?


2003-12-28 08:19:18 AM
jbuilder8
Hi all,
I'm new to Java but learning. Right now I'm trying to generate an XML
document then store it in a String (just one piece of a larger
operation). The specified format is:
<msg><cmd>test</cmd><param>val</param></msg>
But what I'm getting is this:
<?xml version="1.0" encoding="UTF-8"?>
<msg><cmd>test</cmd><param>val</param></msg>
How do I turn off the declaration and keep just the second line?
Here's my current code (snipped from a few different routines for
brevity):
DocumentBuilderFactory Factory;
DocumentBuilder Builder;
try {
Factory = DocumentBuilderFactory.newInstance();
Builder = Factory.newDocumentBuilder();
}
catch (ParserConfigurationException e) {
// TBD: DO SOMETHING WITH THIS
}
Document doc = Builder.newDocument();
Element root = doc.createElement("msg");
Element child = doc.createElement("cmd");
Text text = doc.createTextNode("test");
doc.appendChild(root);
root.appendChild(child);
child.appendChild(text);
try {
Transformer t =
TransformerFactory.newInstance().newTransformer();
DOMSource ds = new DOMSource(doc);
StringWriter sw = new StringWriter();
StreamResult sr = new StreamResult(sw);
t.transform(ds,sr);
String msg = sw.toString();
}
catch (TransformerConfigurationException e) {
// TBD: DO SOMETHING WITH THIS
}
catch (TransformerException e) {
// TBD: DO SOMETHING WITH THIS
}
This seems to work fine, except for the unwanted declaration. All
suggestions welcome, especially ones that include a feasible plan for
early retirement to the tropics :)
 
 

Re:XML output - how to turn off declaration?

On Sun, 28 Dec 2003 00:19:18 GMT, XXXX@XXXXX.COM (Hal Brooks) wrote:
Sorry, I left out a snippet from my code. Here's the code with the
addition (which comes from a subroutine dedicated to adding parameters
to the XML file.):
Quote
DocumentBuilderFactory Factory;
DocumentBuilder Builder;

try {
Factory = DocumentBuilderFactory.newInstance();
Builder = Factory.newDocumentBuilder();
}
catch (ParserConfigurationException e) {
// TBD: DO SOMETHING WITH THIS
}

Document doc = Builder.newDocument();
Element root = doc.createElement("msg");
Element child = doc.createElement("cmd");
Text text = doc.createTextNode("test");

doc.appendChild(root);
root.appendChild(child);
child.appendChild(text);
Element param = doc.createElement("param");
Text text = doc.createTextNode("val");
doc.getDocumentElement().appendChild(param);
param.appendChild(text);
Quote
try {
Transformer t =
TransformerFactory.newInstance().newTransformer();
DOMSource ds = new DOMSource(doc);
StringWriter sw = new StringWriter();
StreamResult sr = new StreamResult(sw);
t.transform(ds,sr);

String msg = sw.toString();
}
catch (TransformerConfigurationException e) {
// TBD: DO SOMETHING WITH THIS
}
catch (TransformerException e) {
// TBD: DO SOMETHING WITH THIS
}