Thank you for your sharp answer... I will implements the first method I think.
Stéphane. -----Message d'origine----- De : Nathan Beyer [mailto:[EMAIL PROTECTED] Envoyé : mardi 22 février 2005 03:09 À : [EMAIL PROTECTED] Objet : RE: Serialisation with no default namespace prefix If the document could be created in any way externally to this serialization, then you're basically going to have to add/modify the current XMLNS declarations to change the one you want to be the default namespace and then dig through all of the nodes in the document and set their prefixes to empty. This is a crude example: DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.newDocument(); Element root = doc.createElementNS("urn:temp:temp","tns:rootElement"); doc.appendChild(root); Element child = doc.createElementNS("urn:temp:temp","tns:childElement"); root.appendChild(child); //serialize with prefixes TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty(OutputKeys.METHOD, "xml"); t.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); t.transform(new DOMSource(doc), new StreamResult (System.err)); //add the XMLNS declaration root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "urn:temp:temp"); //change the prefixes for all nodes root.setPrefix(""); child.setPrefix(""); t.transform(new DOMSource(doc), new StreamResult (System.out)); To make this more robust you'd have to search through DOM looking for the nodes with the namespace you're interested and change only those nodes. Another way would be to create a simple copy XSLT stylesheet that changes the prefixes for you. You could then just load this up as a stylesheet with the transformer APIs and just apply it to any document. These may not be the most efficient approaches, but they might work for you. -Nathan -----Original Message----- From: Stéphane Rault [mailto:[EMAIL PROTECTED] Sent: Monday, February 21, 2005 9:00 AM To: [EMAIL PROTECTED] Subject: RE : Serialisation with no default namespace prefix Do you have a snippet of the code that constructs the elements? I believe you just need to create the namespace attribute declaration correctly. In your example you have 'xmlns:ced="namespace"', but what you want is 'xmlns="namespace"', but that could just be a typo. Y<ou are right for the typo. I made a mistake and what I want is xmlns="namespace". For the production of the code, I want something that is generic. I mean, the doc I used for the example is an XmlBeans production but my code has to work with every DOM Document in the same way. Stéphane. -----Original Message----- From: Stéphane Rault [mailto:[EMAIL PROTECTED] Sent: Monday, February 21, 2005 8:44 AM To: [EMAIL PROTECTED] Subject: Serialisation with no default namespace prefix Hello all. I want to serialize some Xml Documents with namespace inside but with a defaultNamespace and no prefix for this namespace. How can I do it ? // Piece of code OutputFormat format = new OutputFormat("xml", "ISO-8859-1", true); XMLSerializer serialiseur = new XMLSerializer(format); StringWriter sw = new StringWriter(); serialiseur.setOutputCharStream(sw); serialiseur.asDOMSerializer(); serialiseur.setNamespaces(true); serialiseur.serialize(anElement); // End Piece of code If I use it I have an output like that : // What I don't want <?xml version="1.0" encoding="ISO-8859-1"?> <ced:traceReceipt xmlns:ced="MyNamespace"> <ced:taskName>MyContent</ced:taskName> </ced:traceReceipt> // End What I don't want I want to obtain instead : // What I want <?xml version="1.0" encoding="ISO-8859-1"?> <traceReceipt xmlns:ced="MyNamespace"> <taskName>MyContent</taskName> </traceReceipt> // End What I want Thanks in advance --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]