Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

I'm getting an extra, unwanted attribute

Thread view: 
Paul J. Lucas - 18 Feb 2007 20:29 GMT
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/", "rdf:Description"
   );
   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#">
     <rdf:Description 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
roy axenov - 18 Feb 2007 21:20 GMT
On Feb 18, 10:29 pm,
pauljlucas.removet...@removethistoo.mac.com (Paul J. Lucas)
wrote:
> 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.

Disclaimer: last time I coded in Java was years ago. Still,
the problem seems obvious to me.

>     Element xapDescElement = doc.createElementNS(
>         "http://ns.adobe.com/xap/1.0/", "rdf:Description"
>     );

Now, what are you doing here? You're your DOM API asking to
create a 'Description' element in
'http://ns.adobe.com/xap/1.0/' namespace, specifying the
'rdf' namespace prefix. Naturally, the result should look
as:

<rdf:Description xmlns:rdf="http://ns.adobe.com/xap/1.0/"/>

...which is precisely what you're getting.

>     xapDescElement.setAttribute( "rdf:about", "" );
>     xapDescElement.setAttribute( "xmlns:xap",
> "http://ns.adobe.com/xap/1.0/" );

First of all, language lawyers might correct me if get
something wrong, but those xmlns:foo thingies are not
attributes per se, or at least, they are extremely special
attributes. The proper term is namespace declaration nodes,
or something like that. So, I doubt setAttribute() is the
proper way of creating them. And even if does work, what
did you expect to happen? You declaring a namespace with
xap namespace prefix, but the element uses the rdf prefix
anyway.

So the problem is not in the API, the API does precisely
what you asked it to do. The problem is that you seem
either unsure about what you actually want, or you're
misunderstanding something about the way the XML namespaces
work.

Since I'm completely unfamiliar with XMP and RDF, I'm not
sure what you're doing wrong about your data, but I'd
recommend re-reading the specs on those formats (and
probably look up some examples to see how it actually
works).

>   <x:xmpmeta xmlns:x="adobe:ns:meta/">
>     <rdf:RDF
[quoted text clipped - 4 lines]
>     </rdf:RDF>
>   </x:xmpmeta>

Sure, it looks wrong, but it's what you asked for.

> There are two problems:
> 1. The second xmlns:rdf attribute shouldn't be there at
> all.

You're creating an element in that namespace using the rdf
prefix--of course it has to be declared, since the previous
declaration differs.

> 2. The value is wrong: it's the same as xmlns:xap.  How
> did that happen?

It's the namespace you used in createElementNS().

--
roy axenov
Paul J. Lucas - 18 Feb 2007 21:38 GMT
In comp.text.xml roy axenov <r_axenov@mail.ru> wrote:
> On Feb 18, 10:29 pm,
> pauljlucas.removet...@removethistoo.mac.com (Paul J. Lucas)
> wrote:

> Disclaimer: last time I coded in Java was years ago. Still,
> the problem seems obvious to me.
[quoted text clipped - 6 lines]
> create a 'Description' element in 'http://ns.adobe.com/xap/1.0/'
> namespace ...

Oh yeah.  It was just a silly mistake I couldn't see.  Nothing beats
another pair of eyes.  Thanks!

- Paul


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.