document.addContent(new ProcessingStruction("xml-stylesheet","type='text/xsl' href='treefunc.xsl'")hope it will help you.But I think you can use JAXP to transform xml with xsl

解决方案 »

  1.   

    楼上说的对,用ProcessingInstruction(java.lang.String target, java.lang.String data)
      

  2.   

    Document doc=builder.newDocument();
                Node sheet = doc.createProcessingInstruction("xml-stylesheet","type=\"text/xsl\" href=\"'treefunc.xsl\"");
      

  3.   

    Document doc = new Document();
            Element root = new Element("root");
            doc.setRootElement(root);
            ProcessingInstruction pi = new ProcessingInstruction("stylesheet", "");
            pi.setValue("type", "text/xsl");
            pi.setValue("href", "treefunc.xsl");
            doc.addContent(pi);
            XMLOutputter outputter = new XMLOutputter("  ", true);
            outputter.output(doc, System.out);
      

  4.   

    Document doc = new Document();
    编译不过去呀
      

  5.   

    我想生成这样的文件
    <?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"?>请指点!
      

  6.   

    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 produceNewXml()
        {
            try
            {
                org.jdom.input.SAXBuilder sb = new org.jdom.input.SAXBuilder();
                org.jdom.Document doc = sb.build(new java.io.
                                                 StringBufferInputStream(xmlContent));            org.jdom.Document doc2 = new org.jdom.Document();            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 addCommentXml()
        {
            try
            {
                org.jdom.input.SAXBuilder sb = new org.jdom.input.SAXBuilder();
                org.jdom.Document doc = sb.build(new java.io.
                                                 StringBufferInputStream(xmlContent));            org.jdom.Document doc2 = new org.jdom.Document();            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));
                org.jdom.Comment comment = new org.jdom.Comment(" add three instruction!");
                doc.addContent(comment);
                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 | org.jdom.filter.ContentFilter.COMMENT);            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();
    //        addCommentXml();
        }
    }