Include namespace prefix in output DOM XML?

M

Mark McKay

I'm creating a DOM tree with elements containing svg, fo, xlink and
xsl elements. I'm then attempting to write it to a file. While the
local names are being written correctly, the namespace information is
being omitted. If I define

final String NS_SVG = "http://www.w3.org/2000/svg";

I'd like the output to look something like

<root xmlns:svg="http://www.w3.org/2000/svg">

<svg:svg width="372" height="452" viewBox="0 0 372 452">
<svg:rect x="10" y="10" width="352" height="432"
fill="none" stroke="black" stroke-width="2"/>
</svg:svg>

</root>

Is there a way to do this with DOM?

I'm writing it to disk with:

TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
t.setParameter("svg", NS_SVG);
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",
"4");

DOMSource ds = new DOMSource(doc);
StreamResult sr = new StreamResult(ostream);
t.transform(ds, sr);

Mark McKay
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top