用JDOM怎么生成这样的文件?马上结贴!! 谢谢真是纳闷了 为什么只能把<?stylesheet href="treefunc.xsl" type="text/xsl"?>加在末尾 这能用么 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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(); }} 输出:<?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?> <?AA BB?>还不是在末尾吗!!!这个样子的:<?xml version="1.0" encoding="UTF-8"?><?stylesheet href="treefunc.xsl" type="text/xsl"?><root />用JDOM就搞不出来么? 我需要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(); }} 输出:<?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> java网站的中英文切换该怎样实现呀。。 SSH 框架 中 使用 MySql 数据库 插入数据乱码! 在一个java项目中,如果有多个WSDL应该怎么去操作。 泛型方法的问题 struts+hibernate+SqlServer 外键表插入外键时的问题 linux 系统下运行jsp页面! 报“无此网页”的错误! 急切盼望回复!(100分) struts问题,大家来瞧一瞧 郁闷啊,简单的数据库操作出错?????????? 一个最简单的hibernate出错,出错信息如下 部署异常!!!!急救!!! 刚刚开始学习,有个简单问题请教高手? 50分,数据类型转换的问题。
{
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();
}
}
<?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?>
这个样子的:
<?xml version="1.0" encoding="UTF-8"?>
<?stylesheet href="treefunc.xsl" type="text/xsl"?>
<root />
用JDOM就搞不出来么?
程序如下:
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();
}
}
<?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>