aspose file tools*
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
Author

Result is not appropriate

Punya Pratap Singh
Ranch Hand

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

Out put should be ---
<Company>
<Location>
<Companyname></Companyname> like this
---------------------------
But out put is coming as- javax.xml.transform.stream.StreamResult@9444d1

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."
doc.appendChild(root);
Element child = doc.createElement("Location");
root.appendChild(child);
Element child1 = doc.createElement("CompanyName");
child.appendChild(child1);

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

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

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

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

// now also introducing attributes inside the elements.as key value pair

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

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

// 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)));
bw.write(xmlString);
bw.flush();
bw.close();

}


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: 80
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: 80
I tried that bro.but wasn't working.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 542
    
    3
[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.
 
wood burning stoves
 
subject: Result is not appropriate