P
Paul J. Lucas
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 ByteArrayInputStream( bytes );
DocumentBuilder docBuilder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = docBuilder.newDocument();
DOMResult domResult = new DOMResult( doc );
TransformerFactory xformFactory = TransformerFactory.newInstance();
Transformer xform = xformFactory.newTransformer();
xform.transform( new StreamSource( is ), domResult );
Element xapDescElement = doc.createElementNS(
"http://ns.adobe.com/xap/1.0/", "rdfescription"
);
xapDescElement.setAttribute( "rdf:about", "" );
xapDescElement.setAttribute( "xmlns:xap", "http://ns.adobe.com/xap/1.0/" );
Node topRDF = doc.getDocumentElement().getChildNodes().item( 1 );
topRDF.appendChild( xapDescElement );
and when I write it out as text using:
Writer writer = new OutputStreamWriter( System.out, "UTF-8" );
StreamResult streamResult = new StreamResult( writer );
xform = xformFactory.newTransformer();
xform.setOutputProperty( OutputKeys.INDENT, "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#">
<rdfescription 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
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 ByteArrayInputStream( bytes );
DocumentBuilder docBuilder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = docBuilder.newDocument();
DOMResult domResult = new DOMResult( doc );
TransformerFactory xformFactory = TransformerFactory.newInstance();
Transformer xform = xformFactory.newTransformer();
xform.transform( new StreamSource( is ), domResult );
Element xapDescElement = doc.createElementNS(
"http://ns.adobe.com/xap/1.0/", "rdfescription"
);
xapDescElement.setAttribute( "rdf:about", "" );
xapDescElement.setAttribute( "xmlns:xap", "http://ns.adobe.com/xap/1.0/" );
Node topRDF = doc.getDocumentElement().getChildNodes().item( 1 );
topRDF.appendChild( xapDescElement );
and when I write it out as text using:
Writer writer = new OutputStreamWriter( System.out, "UTF-8" );
StreamResult streamResult = new StreamResult( writer );
xform = xformFactory.newTransformer();
xform.setOutputProperty( OutputKeys.INDENT, "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#">
<rdfescription 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