Java XML: how to derive namespace of child elements?

I am using the Java Transformer class to process an XML document object.

This is the code that Transformer creates:

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;

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

transformer.transform(source, result);

Currently, my output is as follows: <svg ... />. I would like it to include the namespace of each element, as in <svg: svg ... />

How can i do this?

+3
source share
3 answers

Please note that <svg xmlns="SVGNS" />matches with <svg:svg xmlns:svg="SVGNS" />.

Have you checked what you called setNamespaceAware(true)on your copy DocumentBuilderFactory?

+2
source

javax.xml.transform , , -, , , .

, , URI .

- , - .

0

, , .

el.setAttribute( "xmi: type", type), el.setAttributeNS( "xsi", "type", type); el.setAttributeNS( " http://www...../URI", "type", type); , NS , . , - xmlns = "..." .

0

Source: https://habr.com/ru/post/1699358/


All Articles