ProcessingInstruction pi = new ProcessingInstruction ("xml-stylesheet","href=\"bookList.html.xsl\" type=\"text/xsl\"");        doc.addContent(pi);

解决方案 »

  1.   

    //加入一条处理指令        
    ProcessingInstruction pi = new ProcessingInstruction 
               ("xml-stylesheet","href=\"bookList.html.xsl\" type=\"text/xsl\""); 
           doc.addContent(pi);
      

  2.   

    我想生成这样的文件
    <?xml version="1.0" encoding="UTF-8"?>
    <?stylesheet href="treefunc.xsl" type="text/xsl"?>
    <root />但用以上的方法出来的都是<?xml version="1.0" encoding="UTF-8"?>
    <root />
    <?stylesheet href="treefunc.xsl" type="text/xsl"?>请指点!
      

  3.   


    其实这条指令加在头和尾都一样!都是在根节点之外!
    至于为什么JDOM把它加在尾部我也不知道!!没有时间去更深入研究!
      

  4.   

    package com.test.csdn.xml;public class TestAddPI
    {
        static String xmlContent;
        public static void init()
        {
            xmlContent = "<?xml version=\"1.0\" encoding=\"GB2312\"?>"
                + "<lib>"
                + "<book>"
                + "<name>Java</name>"
                + "</book>"
                + "<book>"
                + "<name>XML</name>"
                + "<pubdate>2002-10-07</pubdate>"
                + "</book>"
                + "</lib>";
        }    public static void outputXml()
        {
            try
            {
                org.jdom.input.SAXBuilder sb = new org.jdom.input.SAXBuilder();
                org.jdom.Document doc = sb.build(new java.io.
                                                 StringBufferInputStream(xmlContent));
                org.jdom.output.XMLOutputter outer = new org.jdom.output.XMLOutputter();
    //<?stylesheet href="treefunc.xsl" type="text/xsl"?>
                java.util.Map map = new java.util.HashMap();
                map.put("href","treefunc.xsl");
                map.put("type","text/xsl");
                doc.addContent(new org.jdom.ProcessingInstruction("AA","BB"));
                doc.addContent(new org.jdom.ProcessingInstruction("CC","DD"));
                doc.addContent(new org.jdom.ProcessingInstruction("stylesheet",map));            java.io.StringWriter sw = new java.io.StringWriter();
                org.jdom.filter.ContentFilter piFilter = new org.jdom.filter.ContentFilter(
                    org.jdom.filter.ContentFilter.PI );
                org.jdom.filter.ContentFilter dFilter = new org.jdom.filter.ContentFilter(
                    org.jdom.filter.ContentFilter.ELEMENT );            outer.output(doc.getContent(piFilter),sw);
                outer.output(doc.getContent(dFilter),sw);
                sw.close();
                System.out.println(sw.toString());
                //Debug.printTextAsXml(sw.toString());
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }    public static void main(String[] args)
        {
            init();
            outputXml();
        }
    }