JDOM如何生成这样的根元素?? 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 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼上说的对,用ProcessingInstruction(java.lang.String target, java.lang.String data) Document doc=builder.newDocument(); Node sheet = doc.createProcessingInstruction("xml-stylesheet","type=\"text/xsl\" href=\"'treefunc.xsl\""); 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); Document doc = new Document();编译不过去呀 我想生成这样的文件<?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"?>请指点! 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(); }} 工作流引擎应面向开发者还是最终用户? 腾讯开放六位数的QQ免费申请 菜鸟问一个关于c++和java混合编程的问题 想做个程序,希望有人来共同参考一下。 初学多线程,这段代码为什么没有实现方法同步问题?请大侠指教! 请大家推荐一些java方面的网站 classpath 求一个Socket连接池 请问怎样在调用存储过程时使用setArray方法? 怎样给回答我的问题的人送分? weblogic里难道只能调用一个.jsp,其它就不行了吗,郁闷。 编译数据库连接池出错,不知什么原因,请大家帮忙看看,谢谢发,急!!
Node sheet = doc.createProcessingInstruction("xml-stylesheet","type=\"text/xsl\" href=\"'treefunc.xsl\"");
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);
编译不过去呀
<?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"?>请指点!
{
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();
}
}