[jdom-interest] Why PI are outputed after the root element?

Jason Hunter jhunter at collab.net
Sun Dec 10 12:24:16 PST 2000


This is a frequently asked question.  Since you added the PIs after the
root (since the root was added in the document constructor) the PIs are
placed after the root.  Use doc.setMixedContent() if you want to have
full control over placement.

-jh-

Peer Haja wrote:
> 
> Hi all,
> 
> I have a problem with my code. All the porcessing
> instructions are printed after the root element. And
> it seems that the xerces validating parser does not
> like it very much. I have searched the archive list
> and seems no other people have such problem. So it
> must be something wrong with my code. Here it is:
> 
> ns = Namespace.getNamespace("ns",
> "http://www.my.com");
> rootElement = new Element("rootElement", ns);
> Attribute attr =
>     new Attribute ("noNamespaceSchemaLocation",
>                  "http://www.my.com/schema.xsd", ns);
> rootElement.addAttribute(attr);
> Document doc = new Document(rootElement);
> Map map = new HashMap();
> map.put("href", "XSL/html.xsl");
> map.put("type", "text/xsl");
> ProcessingInstruction pi = new
>     ProcessingInstruction("xml-stylesheet", map);
> doc.addContent(pi);
> map = new HashMap();
> map.put("href", "XSL/wml.xsl");
> map.put("type", "text/xsl");
> map.put("media", "wap");
> pi = new
>     ProcessingInstruction("xml-stylesheet", map);
> doc.addContent(pi);
> 
> // add children of the root element here
> 
> XMLOutputter fmt = new XMLOutputter(" ", true);
> fmt.output(doc, System.out);
> 
> The result looks like:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> 
> <ns:rootElement xmlns:ns="http://www.my.com"
> ns:noNamespaceSchemaLocation="http://www.my.com/schema.xsd">
> <!-- children are here -->
>   <ns:child/>
> </ns:rootElement>
> <?xml-stylesheet href="XSL/html.xsl" type="text/xsl"?>
> <?xml-stylesheet href="XSL/wml.xsl" type="text/xsl"
> media="wap"?>
> 
> Thanks,
> 
> David
> 
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Shopping - Thousands of Stores. Millions of Products.
> http://shopping.yahoo.com/
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com



More information about the jdom-interest mailing list