Trouble converting an XML doc into a string

L

laredotornado

Hi,

I'm using Java 1.5, dom4j 1.6, axis 1.4, cglib 2.1_3, and commons-
beanutils 1.7.0. I'm trying to figure out why converting an XML
document to a string is not working. I'm trying to build a very
simple XML doc like so ...

private Document createXMLDocument(String firstName, String
lastName, String number, String year, String month,
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();

Element documentRoot = document.createElement("response");
document.appendChild(documentRoot);
Element documentElement = document.createElement("firstname");
documentElement.setTextContent(firstName);
documentRoot.appendChild(documentElement);
documentElement = document.createElement("lastname");
documentElement.setTextContent(lastName);
documentRoot.appendChild(documentElement);
documentElement = document.createElement("num");
documentElement.setTextContent(number);
documentRoot.appendChild(documentElement);
documentElement = document.createElement("type");
documentElement.setTextContent(type);
documentRoot.appendChild(documentElement);
documentElement = document.createElement("year");
documentElement.setTextContent(year);
documentRoot.appendChild(documentElement);
documentElement = document.createElement("month");
documentElement.setTextContent(month);
documentRoot.appendChild(documentElement);
return document;
}

but trying to get the doc as a string produces a partial document
(e.g. "<?xml version="1.0" encoding="UTF-8"?
<response><firstname>DAVE")

private String getXMLString(Document document) throws Exception {
StringWriter swriter = new StringWriter();
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(swriter);
transformer.transform(source, result);
swriter.close();
return swriter.toString();
}

Any ideas why or how to troubleshoot further? Thanks, - Dave
 
J

Jeff Higgins

laredotornado said:
Hi,

I'm using Java 1.5, dom4j 1.6, axis 1.4, cglib 2.1_3, and commons-
beanutils 1.7.0. I'm trying to figure out why converting an XML
document to a string is not working. I'm trying to build a very
simple XML doc like so ...

private Document createXMLDocument(String firstName, String
lastName, String number, String year, String month,
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();

This doesn't look like it should compile without errors and/or warnings.

Element documentRoot = document.createElement("response");
document.appendChild(documentRoot);
Element documentElement = document.createElement("firstname");
documentElement.setTextContent(firstName);
documentRoot.appendChild(documentElement);
documentElement = document.createElement("lastname");
documentElement.setTextContent(lastName);
documentRoot.appendChild(documentElement);
documentElement = document.createElement("num");
documentElement.setTextContent(number);
documentRoot.appendChild(documentElement);
documentElement = document.createElement("type");
documentElement.setTextContent(type);

Where is type defined?
 
M

Mike Schilling

laredotornado said:
but trying to get the doc as a string produces a partial document
(e.g. "<?xml version="1.0" encoding="UTF-8"?

No idea what your problem is. Having corrected the typos and run it, I see

<?xml version="1.0"
encoding="UTF-8"?><response><firstname>a</firstname><lastname>b</lastname><num>c</num><type>f</type><year>d</year><month>e</month></response>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top