user3355101
user3355101

Reputation: 231

how to print double quotes in xml attribute value

Hi i am generating a xml file using javax.xml parsers able to generate a xml file. But in my attribute value i was getting &quot instead of double quote. How to print double quotes in attribute value. Below is my code

Document doc = docBuilder.newDocument();
            Element rootElement = doc.createElement("elements");
            doc.appendChild(rootElement);
            rootElement.setAttribute("area", "area");
            rootElement.setAttribute("page", "pagename");

            //element
            Element element = doc.createElement("element");
            rootElement.appendChild(element);
            element.setAttribute("key", "key");
            element.setAttribute("id", "id");
            element.setAttribute("path", "//*[@id="email"]");
            }
                    // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File(ApplicationContext.getPath()+File.separator+"test.xml"));

            // Output to console for testing
            // StreamResult result = new StreamResult(System.out);

            transformer.transform(source, result);

Output :
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<elements area="area" page="pagename">
<element id="id" key="key" path="//*[@id=&quot;email&quot;]"/>
</elements>

Expected output:
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <elements area="area" page="pagename">
    <element id="id" key="key" path="//*[@id="email"]"/>
    </elements>

Thanks inadvance

Upvotes: 1

Views: 1718

Answers (1)

Michael Kay
Michael Kay

Reputation: 163342

The output you are trying to produce is not well-formed XML, and no XML parser will accept it. If you want to produce stuff that isn't XML then you can do so, of course, but XML-aware tools will try very hard to prevent it.

Upvotes: 2

Related Questions