Filip Jirsák
Sun, 02 Nov 2008 06:05:47 -0800
Try this:
DocumentFactory documentFactory = new DocumentFactory();
Element el = documentFactory.createElement(QName.get("OAI-PMH", "",
"http://www.openarchives.org/OAI/2.0/"));
el.addAttribute(
QName.get("schemaLocation", "xsi",
"http://www.w3.org/2001/XMLSchema-instance"),
"http://www.openarchives.org/OAI/2.0/
http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd");
Document document = documentFactory.createDocument(el);
System.out.println(document.asXML()); Or you can use Namespace object instead of three-param QName.get() method, or you can use "qualified name" and namespace-to-prefix map of document factory. Filip Jirsák 2008/11/2 Alessio Camaroto <[EMAIL PROTECTED]>: > It works thanks a lot but if I want to put others namespaces ? I don't want > this case: > > <OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > > xmlns:xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ > http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"> > > I need > > <OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ > http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"> > > How can I do it ? > > Thanks > Alessio > > 2008/11/2 Filip Jirsák <[EMAIL PROTECTED]> >> >> I'm sorry, it seems Document.Factory.createNamespace() works slightly >> different from new Namespace(). So instead of null namespace prefix >> use empty string: >> >> Namespace ns = documentFactory.createNamespace("", >> "http://www.w3.org/2005/Atom"); >> >> Following code works for me: >> >> public static void main(String[] args) { >> DocumentFactory documentFactory = new DocumentFactory(); >> Namespace ns = >> documentFactory.createNamespace("","http://www.w3.org/2005/Atom"); >> Element el = documentFactory.createElement(QName.get("root", ns)); >> Document document = documentFactory.createDocument(el); >> >> System.out.println(document.asXML()); >> } >> >> Output of this code is: >> >> <?xml version="1.0" encoding="UTF-8"?> >> <root xmlns="http://www.w3.org/2005/Atom"/> >> >> Filip Jirsák >> >> 2008/11/2 Alessio Camaroto <[EMAIL PROTECTED]>: >> > Thanks a lot but It doesn't work I have a java.lang.NullPointerException >> > when I try to create the Namespace ns. Any other idea ? >> > >> > Thanks again >> > Alessio >> > >> > 2008/11/1 Filip Jirsák <[EMAIL PROTECTED]> >> >> >> >> Hello, >> >> can you give us example of your source code? I think this should work >> >> as you expect: >> >> >> >> Namespace ns = documentFactory.createNamespace(null, >> >> "http://www.w3.org/2005/Atom"); >> >> Element el = documentFactory.createElement(QName.get("root", ns)); >> >> documentFactory.createDocument(el).asXML(); >> >> >> >> Filip Jirsák >> >> >> >> 2008/11/1 Alessio Camaroto <[EMAIL PROTECTED]>: >> >> > Hi I'm Alessio, >> >> > I have a problem with XML Namespaces. I need to write an XML file >> >> > with >> >> > the >> >> > follow namespace "xmlns=http://www.w3.org/2005/Atom" but if I use the >> >> > standard API's I obtain: >> >> > >> >> > - xmlns:xmlns="http://www.w3.org/2005/Atom" --> with prefix="xmlns" >> >> > and >> >> > suffix="http://www.w3.org/2005/Atom" values >> >> > >> >> > or >> >> > >> >> > - xmlns="http://www.w3.org/2005/Atom" and xmlns="" for each node >> >> > child >> >> > --> >> >> > only suffix="http://www.w3.org/2005/Atom" value >> >> > >> >> > Is there a solution to my problem ? What's wrong ? >> >> > >> >> > Thanks a lot >> >> > Alessio >> >> > >> >> > >> >> > >> >> > ------------------------------------------------------------------------- >> >> > This SF.Net email is sponsored by the Moblin Your Move Developer's >> >> > challenge >> >> > Build the coolest Linux based applications with Moblin SDK & win >> >> > great >> >> > prizes >> >> > Grand prize is a trip for two to an Open Source event anywhere in the >> >> > world >> >> > http://moblin-contest.org/redirect.php?banner_id=100&url=/ >> >> > _______________________________________________ >> >> > dom4j-user mailing list >> >> > dom4j-user@lists.sourceforge.net >> >> > https://lists.sourceforge.net/lists/listinfo/dom4j-user >> >> > >> >> > >> >> >> >> >> >> >> >> -- >> >> Filip Jirsák >> >> [EMAIL PROTECTED] >> > >> > >> >> >> >> -- >> Filip Jirsák >> [EMAIL PROTECTED] > > -- Filip Jirsák [EMAIL PROTECTED] ------------------------------------------------------------------------- This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world http://moblin-contest.org/redirect.php?banner_id=100&url=/ _______________________________________________ dom4j-user mailing list dom4j-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/dom4j-user