File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes Result is not appropriate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Reply locked New topic

Result is not appropriate

Punya Pratap Singh
Ranch Hand

Joined: Nov 23, 2010
Posts: 81
As I am running this code It is working fine but the out put in xml file is not appropriate.

Out put should be ---
<Companyname></Companyname> like this
But out put is coming as-

please help me as I am new in XML.

public void createXmlTree(org.w3c.dom.Document doc) throws Exception {
// this method creates a element node.
Element root = doc.createElement("Company");
// "company is the the first child of xml doc ,means root node."
Element child = doc.createElement("Location");
Element child1 = doc.createElement("CompanyName");

// now we will create comment with the help of doc.

Comment comment = doc.createComment("Employee in Associates");

Element element = doc.createElement("Employee");

// now we will create TextNode with the help of doc.and then we will
// place it under element
Text text12 = doc.createTextNode("Anuraag Singh");

// now also introducing attributes inside the key value pair

Element el1 = doc.createElement("Id");
el1.setAttribute("name", "Aish");

Text tx23 = doc.createTextNode("Status");

// now we use transformer object to write the source tree.

TransformerFactory transFact = TransformerFactory.newInstance();
Transformer transformer = transFact.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");

// as dom tree we have already created now we will access String from
// there.A character stream that collects its output in a string buffer,
// which can then be used to
// construct a string.

StringWriter stwriter = new StringWriter();
StreamResult result = new StreamResult(stwriter);
// as we have written all the things in doc which is a xml document so
// we are passing it here.
DOMSource source = new DOMSource(doc);
System.out.println("String " + doc);
transformer.transform(source, result);
String xmlString = result.toString();

File file = new File("C:/WebApplications/newxml.xml");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file)));


Santhosh ayiappan
Ranch Hand

Joined: Jan 30, 2007
Posts: 80

Don't create 2 seperate threads for the same issue.
Punya Pratap Singh
Ranch Hand

Joined: Nov 23, 2010
Posts: 81
Thanks for your update,I will remember it.please also post some reply for the issue. Thanks.
Santhosh ayiappan
Ranch Hand

Joined: Jan 30, 2007
Posts: 80

I have responded to your other thread. Please check it.
Punya Pratap Singh
Ranch Hand

Joined: Nov 23, 2010
Posts: 81
I tried that bro.but wasn't working.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 630
[1] The last part of the code seems turning round and round a central purpose... You can do simply this, replacing the whole block starting from StringWriter... onword.

[2] What Santhosh was telling you is to replace the right-hand side of that xmlString line. It makes a lot of sense and should do.
I agree. Here's the link:
subject: Result is not appropriate
It's not a secret anymore!