I have code that builds a DOM using the Java org.w3c.dom API and it adds an
extra attribute to one of the elements that I don't want. Not only that, the
value it adds is wrong.
The code to create the DOM is:
String emptyXMPString =
"<x:xmpmeta xmlns:x='adobe: ns:meta/'>\n" +
" <rdf:RDF xmlns:rdf='http ://www.w3.org/1999/02/22-rdf-syntax-ns#'/>\n" +
"</x:xmpmeta>\n";
byte[] bytes = emptyXMPString. getBytes( "UTF-8" );
InputStream is = new ByteArrayInputS tream( bytes );
DocumentBuilder docBuilder =
DocumentBuilder Factory.newInst ance().newDocum entBuilder();
Document doc = docBuilder.newD ocument();
DOMResult domResult = new DOMResult( doc );
TransformerFact ory xformFactory = TransformerFact ory.newInstance ();
Transformer xform = xformFactory.ne wTransformer();
xform.transform ( new StreamSource( is ), domResult );
Element xapDescElement = doc.createEleme ntNS(
"http://ns.adobe.com/xap/1.0/", "rdf:Descriptio n"
);
xapDescElement. setAttribute( "rdf:about" , "" );
xapDescElement. setAttribute( "xmlns:xap" , "http://ns.adobe.com/xap/1.0/" );
Node topRDF = doc.getDocument Element().getCh ildNodes().item ( 1 );
topRDF.appendCh ild( xapDescElement );
and when I write it out as text using:
Writer writer = new OutputStreamWri ter( System.out, "UTF-8" );
StreamResult streamResult = new StreamResult( writer );
xform = xformFactory.ne wTransformer();
xform.setOutput Property( OutputKeys.INDE NT, "yes" );
xform.transform ( new DOMSource( doc ), streamResult );
I get:
<x:xmpmeta xmlns:x="adobe: ns:meta/">
<rdf:RDF xmlns:rdf="http ://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Descriptio n xmlns:xap="http ://ns.adobe.com/xap/1.0/" rdf:about=""
xmlns:rdf="http ://ns.adobe.com/xap/1.0/"/>
</rdf:RDF>
</x:xmpmeta>
There are two problems:
1. The second xmlns:rdf attribute shouldn't be there at all.
2. The value is wrong: it's the same as xmlns:xap. How did that happen?
I'd prefer to get tid of the extra xmlns:rdf attribute completely, but, if
I can't, how can I get it to at least be the correct value?
- Paul