谢谢真是纳闷了 为什么只能把
<?stylesheet href="treefunc.xsl" type="text/xsl"?>
加在末尾 这能用么

解决方案 »

  1.   

    public class TestReplace
    {
        static String xmlContent;
        public static void init()
        {
            xmlContent = "<?xml version=\"1.0\" encoding=\"GB2312\"?>"
                + "<lib>"
                + "<book>"
                + "<name>Java</name>"
                + "<author>zhang</author>"
                + "<pub>qinghua</pub>"
                + "<price>35.0</price>"
                + "<pubdate>2002-10-07</pubdate>"
                + "</book>"
                + "<book>"
                + "<name>XML</name>"
                + "<author>li</author>"
                + "<pub>hope</pub>"
                + "<price>92.0</price>"
                + "<pubdate>2002-10-07</pubdate>"
                + "</book>"
                + "</lib>";
        }    public static void replaceXML()
        {
            try
            {
                org.jdom.input.SAXBuilder sb = new org.jdom.input.SAXBuilder();
                System.out.println(xmlContent);
                org.jdom.Document doc = sb.build(new java.io.
                                                 StringBufferInputStream(xmlContent));
                org.jdom.Element root = doc.getRootElement(); //得到根元素
                java.util.List books = root.getChildren(); //得到根元素所有子元素的集合
                org.jdom.Element book = null;
                for (int i = 0; i < books.size(); i++)
                {
                    book = (org.jdom.Element) books.get(i); //得到第一本书元素
                    book.getChild("price").setText("100");
                }
                org.jdom.output.XMLOutputter outer = new org.jdom.output.
                    XMLOutputter();            org.jdom.ProcessingInstruction pi = new
                    org.jdom.ProcessingInstruction("AA","BB");
                doc.addContent(pi);            java.io.StringWriter sw = new java.io.StringWriter();
                outer.output(doc.getContent(),sw);
                sw.close();
    //            outer.output(doc, sw);
                System.out.println("----------------");
                System.out.println(sw.toString());
                //Debug.printTextAsXml(sw.toString());
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }    public static void main(String[] args)
        {
            init();
            replaceXML();
        }
    }
      

  2.   

    输出:
    <?xml version="1.0" encoding="GB2312"?><lib><book><name>Java</name><author>zhang</author><pub>qinghua</pub><price>35.0</price><pubdate>2002-10-07</pubdate></book><book><name>XML</name><author>li</author><pub>hope</pub><price>92.0</price><pubdate>2002-10-07</pubdate></book></lib>----------------<lib><book><name>Java</name><author>zhang</author><pub>qinghua</pub><price>100</price><pubdate>2002-10-07</pubdate></book><book><name>XML</name><author>li</author><pub>hope</pub><price>100</price><pubdate>2002-10-07</pubdate></book></lib><?AA BB?>
      

  3.   

    <?AA BB?>还不是在末尾吗!!!
    这个样子的:
    <?xml version="1.0" encoding="UTF-8"?>
    <?stylesheet href="treefunc.xsl" type="text/xsl"?>
    <root />
    用JDOM就搞不出来么?
      

  4.   

    我需要300分!
    程序如下:
    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 replaceXML()
        {
            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();
            replaceXML();
        }
    }
      

  5.   

    输出:
    <?AA BB?><?CC DD?><?stylesheet href="treefunc.xsl" type="text/xsl"?><lib><book><name>Java</name></book><book><name>XML</name><pubdate>2002-10-07</pubdate></book></lib>