有哪位高手用过JDOM啊?! http://www.csdn.net/develop/Read_Article.asp?Id=19171 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Element.addContent();http://www.jdom.org/docs/apidocs/index.html 谢谢你们,我是参考以那些资料的,但有问题:testXml.java: package cn.vad.basalservice.dbop; import org.jdom.*; import org.jdom.input.*; import org.jdom.output.*; import java.io.*; import java.util.*; public class testXml { public org.jdom.Document aDoc = new Document(); public org.jdom.Element rootEle; public String strRootEle = "RootElement"; public String strOutputPath = "D:\\jsp\\testok.xml"; public testXml() { //向根元素中增加子元素 aDoc.setRootElement( new Element( strRootEle ) ); rootEle = aDoc.getRootElement(); addRootElement(); outputXml(); } public void outputXml() { try { //org.jdom.output.XMLOutputter xmlOutputter = new XMLOutputter( "",true,"GB2312" ); //xmlOutputter.output( aDoc,new FileOutputStream(this.strOutputPath) ); //亦可 XMLOutputter outputter = new XMLOutputter(" ", true); outputter.output(aDoc, System.out); FileWriter writer = new FileWriter(this.strOutputPath); outputter.output(aDoc, writer); writer.close(); } catch( IOException e ) { System.out.println(e.getMessage()); } } public void addRootElement() { rootEle.addContent( new Element("testRoot2").addContent("testok_02") ); rootEle.addContent( new Element("testRoot3").addContent("testok_03") ); //如果加上一行就会写两个元素到XML中,如果不加就一条 //说明每写一次都是将XML中的内容清掉,再重写。现在需要不断追加该怎么办呢?! } public void addElement( String str,String val ) { rootEle.addContent( new Element(str).addContent( val ) ); outputXml(); } public void removeElement() { rootEle.removeChild( "str" ); outputXml(); } } public testXml() { try { File strFile = new File( strOutputPath ); if( strFile.exists() ) { aDoc = sb.build( new FileInputStream( strFile ) ); //outputXml(); } else { aDoc = new Document(); aDoc.setRootElement( new Element( strRootEle ) ); } }catch(Exception e ){ System.out.println( e.getMessage() ); } rootEle = aDoc.getRootElement(); ll = rootEle.getChildren(); }//ok BTrace问题 struts2 <s:if>的问题 本人最近想学java,有谁知道武汉有哪个好点的java培训学校啊 折腾了一个下午也没结果。spring抛出java.util.ConcurrentModificationException? 大虾来给我讲点基础的 200分请教高手~~JBoss5怎么热发布~顶者有分 cluster中quartz的调度问题请教。 Structs引入bean时报空指针异常 unix下maven解析scss出错 shiro+ehcache+session管理问题 j2ee开发工程师薪资调查 这是什么错误呢,有关JNDI的
http://www.jdom.org/docs/apidocs/index.html
import org.jdom.input.*;
import org.jdom.output.*;
import java.io.*;
import java.util.*; public class testXml
{
public org.jdom.Document aDoc = new Document();
public org.jdom.Element rootEle; public String strRootEle = "RootElement";
public String strOutputPath = "D:\\jsp\\testok.xml"; public testXml()
{
//向根元素中增加子元素
aDoc.setRootElement( new Element( strRootEle ) );
rootEle = aDoc.getRootElement(); addRootElement(); outputXml(); } public void outputXml()
{
try
{
//org.jdom.output.XMLOutputter xmlOutputter = new XMLOutputter( "",true,"GB2312" );
//xmlOutputter.output( aDoc,new FileOutputStream(this.strOutputPath) ); //亦可
XMLOutputter outputter = new XMLOutputter(" ", true);
outputter.output(aDoc, System.out);
FileWriter writer = new FileWriter(this.strOutputPath);
outputter.output(aDoc, writer);
writer.close(); }
catch( IOException e )
{
System.out.println(e.getMessage());
}
} public void addRootElement()
{
rootEle.addContent( new Element("testRoot2").addContent("testok_02") );
rootEle.addContent( new Element("testRoot3").addContent("testok_03") );
//如果加上一行就会写两个元素到XML中,如果不加就一条
//说明每写一次都是将XML中的内容清掉,再重写。现在需要不断追加该怎么办呢?!
} public void addElement( String str,String val )
{
rootEle.addContent( new Element(str).addContent( val ) ); outputXml();
} public void removeElement()
{
rootEle.removeChild( "str" );
outputXml();
} }
{
try
{
File strFile = new File( strOutputPath );
if( strFile.exists() )
{
aDoc = sb.build( new FileInputStream( strFile ) );
//outputXml();
}
else
{
aDoc = new Document();
aDoc.setRootElement( new Element( strRootEle ) );
}
}catch(Exception e ){ System.out.println( e.getMessage() ); }
rootEle = aDoc.getRootElement();
ll = rootEle.getChildren();
}
//ok