Java Transformer outputs < and > instead of <>
Asked Answered
C

3

7

I am editing an XML file in Java with a Transformer by adding more nodes. The old XML code is unchanged but the new XML nodes have &lt; and &gt; instead of <> and are on the same line. How do I get <> instead of &lt; and &gt; and how do I get line breaks after the new nodes. I already read several similar threads but wasn't able to get the right formatting. Here is the relevant portion of the code:

// Read the XML file

DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();   
DocumentBuilder db = dbf.newDocumentBuilder();   
Document doc=db.parse(xmlFile.getAbsoluteFile());
Element root = doc.getDocumentElement();


// create a new node
Element newNode = doc.createElement("Item");

// add it to the root node
root.appendChild(newNode);

// create a new attribute
Attr attribute = doc.createAttribute("Name");

// assign the attribute a value
attribute.setValue("Test...");

// add the attribute to the new node
newNode.setAttributeNode(attribute);



// transform the XML
Transformer transformer = TransformerFactory.newInstance().newTransformer();   
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
StreamResult result = new StreamResult(new FileWriter(xmlFile.getAbsoluteFile()));   
DOMSource source = new DOMSource(doc);   
transformer.transform(source, result);

Thanks

Crumble answered 24/6, 2013 at 17:1 Comment(3)
can you show a small sample input and a small sample output?Kernite
There is no mention of either < or > in the above code. How are you injecting them?Lovelady
Give us a clue! Show us some angle brackets...Borrego
K
5

based on a question posted here:

public void writeToOutputStream(Document fDoc, OutputStream out) throws Exception {
    fDoc.setXmlStandalone(true);
    DOMSource docSource = new DOMSource(fDoc);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.INDENT, "no");
    transformer.transform(docSource, new StreamResult(out));
}

produces:

<?xml version="1.0" encoding="UTF-8"?>

The differences I see:

fDoc.setXmlStandalone(true);
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
Kernite answered 24/6, 2013 at 17:6 Comment(0)
O
8

To replace the &gt and other tags you can use org.apache.commons.lang3:

StringEscapeUtils.unescapeXml(resp.toString());

After that you can use the following property of transformer for having line breaks in your xml:

transformer.setOutputProperty(OutputKeys.INDENT, "yes");
Outspan answered 11/2, 2014 at 12:57 Comment(0)
K
5

based on a question posted here:

public void writeToOutputStream(Document fDoc, OutputStream out) throws Exception {
    fDoc.setXmlStandalone(true);
    DOMSource docSource = new DOMSource(fDoc);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.INDENT, "no");
    transformer.transform(docSource, new StreamResult(out));
}

produces:

<?xml version="1.0" encoding="UTF-8"?>

The differences I see:

fDoc.setXmlStandalone(true);
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
Kernite answered 24/6, 2013 at 17:6 Comment(0)
C
1

Try passing InputStream instead of Writer to StreamResult.

StreamResult result = new StreamResult(new FileInputStream(xmlFile.getAbsoluteFile()));

The Transformer documentation also suggests that.

Correna answered 24/6, 2013 at 17:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.