On Thu, 2004-09-16 at 00:05, Raul Benito wrote:
> What do you mean for root element?

For the top-level element in the document. If I create the first element
by doing:

Element root = thisDocument.createElementNS(namespace, prefix + ":" +
tag);
root.setAttributeNS(xmlnsnamespace, "xmlns:" + prefix, namespace);
thisDocument.appendChild(root);

Then the first element gets *two* xmlns:prefix=namespace attributes when
I output it via the serializer, like so:

<frog:solicitation xmlns:frog="urn:frog" Id="solicitation-0"
    NS1:schemaLocation="urn:frog http://xml.rcpt.to/mikolaj/default";
    xmlns:NS1="http://www.w3.org/2001/XMLSchema-instance";
xmlns:frog="urn:frog">
    <frog:DNS xmlns:frog="urn:frog">www.rcpt.to</frog:DNS>
    <frog:DNS xmlns:frog="urn:frog">fish.rcpt.to</frog:DNS>
    <frog:Address xmlns:frog="urn:frog">192.168.24.166</frog:Address>
    <frog:Address xmlns:frog="urn:frog">192.168.24.167</frog:Address>
</frog:solicitation>

... and then when trying to parse this again, it bombs with:

Starting [EMAIL PROTECTED]
XML parse fatalError org.xml.sax.SAXParseException: Attribute "frog"
bound to namespace "http://www.w3.org/2000/xmlns/"; was already specified
for element "frog:solicitation".


> First of all, this is a plain DOM question, nothing related with xml-enc
> thing.  Ok the DOM creating interface is not well desined regarding
> namespace issues. First they created without having namespaces in mind,
> and latter they try to fix it. But in my opinion not doing a good job. So
> let me explain. Everytime you do createElementNS you just create an
> element nothing more, nothing else. No "xmlns=x" attribute, no nothing.

I understand what you're saying; you're saying that the extra xmlns=x
that I'm seeing is inserted via the serializer. How can I avoid this? I
basically need to generate and sign a document, pass it across the
network to another location where it will be validated, then embedded
into a new document which will in turn be signed etc.

How do I avoid getting two xmlns=x attributes? Is there an alternative
way to output the DOM as text? I'm currently doing:

        public String toString() {
                OutputFormat of = new OutputFormat(thisDocument, "UTF-8", true);
                StringWriter sw = new StringWriter();
                XMLSerializer xs = new XMLSerializer(sw, of);
                xs.setNamespaces(true);
                try {
                        xs.asDOMSerializer();
                        xs.serialize(thisDocument.getDocumentElement());
                } catch(IOException e) {
                        sw.write("IOException " + e.getMessage());
                }       
                return sw.toString();                   
        }       


Hmm, looking at that, removing the xs.setNamespaces(true) does seem to
solve the immediate problem, but I can't believe that it's the right
solution! I'm going to play with this for a bit, in the hope that it
will make it work.

> The problems you are having is that the xercer serializer add all the
> prefix and xmlns definitions for you. But this is not what the xml-sec is
> specting(If you want to fix it, feel free to send the patches). So don't
> think your dom tree is right because you pretty serilaze it and looks
> fine, is sure you are forgetting xmlns binding that are added by the
> xerces serializer.

Okay, so how do I actually extract the exact DOM tree that I'm
processing as text, if the serializer is going to modify it? I've been
assuming that:

s = new Solicitation(new InputSource(new StringReader(s.toString())));

where the Solicitation(String) constructor basically runs the String
through DocumentBuilder.parse, is basically a no-op, but if you're
telling me that this is not the case, then I'm at something of a loss!

m.


Reply via email to