有关xml增加一条记录的问题!!! Element publish = doc.createElement("publish"); publish.appendChild(doc.createTextNode("南京大学")); doc.appendChild(publish);doc是文档,是一个Nodelist 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼主的XML写错了吧。doc是文档,是一个Nodelist???doc应该是Document吧。所以楼上的doc.appendChild(publish)只会把publish加到<bookList/>的兄弟结点,从而使整个XML变成非well-formed的(含有2个根元素)。 NodeList nl = doc.getNodeByTagName("book2"); //这是按第一个book2变成book做的Node node = nl.item(0);Element publish = node.createElement("publish");publish.appendChild(doc.createTextNode("南京大学"));node.appendChild(publish);另外<book2> ????? <name>Java</name> <author>老朱</author> <publishDate>2002-6-6</publishDate> <price>35.0</price> <publish>南京大学</publish> </book1> </book2> ????? </book> ?????这写错了要么把最上面的2去掉,要么把里面的去掉 各位,我用的是jdom的包,里面没有你们所说的那些方法阿,addContent这个方法可不可以用阿,该怎么用阿???帮忙阿!!! 你的xml严重有错误.jdom里面也有啊Document doc(这里和你的xml文件挂起来)然后根据doc得到book的Element e_bookElement e_publish=new Element("南京大学");e_book.addElement(e_publish);就可以对doc进行写操作了. 首先把<?xml version="1.0" encoding="GBK"?> <bookList> <book id ="111"> <book1> <book2> <name>Java</name> <author>老朱</author> <publishDate>2002-6-6</publishDate> <price>35.0</price> </book1> </book2> </book> </bookList> 改为:<?xml version="1.0" encoding="UTF-8"?><bookList> <book id ="111"> <book1> <book2> <name>Java</name> <author>老朱</author> <publishDate>2002-6-6</publishDate> <price>35.0</price> </book2> </book1> </book> </bookList> import javax.xml.parsers.*;import javax.xml.transform.*;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import org.w3c.dom.*;public class test { public static void main(String args[]){ try{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document doc=builder.parse("E:/test.xml"); doc.normalize(); Text textseg; Element ebook2=(Element)doc.getElementsByTagName("book2").item(0); Element epublic=doc.createElement("publish"); textseg=doc.createTextNode("南京大学"); epublic.appendChild(textseg); ebook2.appendChild(epublic); TransformerFactory tFactory =TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new java.io.File("E:/test.xml")); transformer.transform(source, result); }catch(Exception ex){ ex.printStackTrace(); } }} 这个问题有解吗???元素发生嵌套了啊,XML解析器能过吗???<?xml version="1.0" encoding="GBK"?> <bookList> <book id ="111"> <book1/> <book2> <name>Java</name> <author>老朱</author> <publishDate>2002-6-6</publishDate> <price>35.0</price> </book2> </book> </bookList> 这样就对了。在JDOM里这样写。Element element = new Element( "publish" );element.setText( "南京大学" );doc.getRootElement( "bookList" ).getChild( "book2" ).addaddContent( element );OK!!! 又是菜鸟求助 SSH框架下自动启动定时任务 action中发两次请求的问题 求教,如何将基于ibatis的jpetstore工程改写为基于hibernate... 在jsp使用框架1!!急 谁能给我一个级联保存与更新的完整例子(谢谢!) 关于appfuse2下载的问题!急求100分 哪个朋友用appfuse做过项目????? 来这里发贴问struts的基础问题 JBOSS 一个奇怪的bmp实体EJB问题,在线等待........... 在jbuilder8中,为什么编译通过,运行时会出现NoClassDefFoundError异常?
doc应该是Document吧。
所以楼上的doc.appendChild(publish)只会把publish加到<bookList/>的兄弟结点,从而使整个XML变成非well-formed的(含有2个根元素)。
Node node = nl.item(0);
Element publish = node.createElement("publish");
publish.appendChild(doc.createTextNode("南京大学"));
node.appendChild(publish);
另外
<book2> ?????
<name>Java</name>
<author>老朱</author>
<publishDate>2002-6-6</publishDate>
<price>35.0</price>
<publish>南京大学</publish>
</book1>
</book2> ?????
</book> ?????
这写错了
要么把最上面的2去掉,要么把里面的去掉
jdom里面也有啊
Document doc(这里和你的xml文件挂起来)
然后根据doc得到book的Element e_book
Element e_publish=new Element("南京大学");
e_book.addElement(e_publish);
就可以对doc进行写操作了.
<?xml version="1.0" encoding="GBK"?>
<bookList>
<book id ="111">
<book1>
<book2>
<name>Java</name>
<author>老朱</author>
<publishDate>2002-6-6</publishDate>
<price>35.0</price>
</book1>
</book2>
</book>
</bookList>
改为:
<?xml version="1.0" encoding="UTF-8"?>
<bookList>
<book id ="111">
<book1>
<book2>
<name>Java</name>
<author>老朱</author>
<publishDate>2002-6-6</publishDate>
<price>35.0</price>
</book2>
</book1>
</book>
</bookList> import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
public class test { public static void main(String args[]){
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse("E:/test.xml");
doc.normalize();
Text textseg;
Element ebook2=(Element)doc.getElementsByTagName("book2").item(0); Element epublic=doc.createElement("publish");
textseg=doc.createTextNode("南京大学");
epublic.appendChild(textseg);
ebook2.appendChild(epublic);
TransformerFactory tFactory =TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new java.io.File("E:/test.xml"));
transformer.transform(source, result);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
<?xml version="1.0" encoding="GBK"?>
<bookList>
<book id ="111">
<book1/>
<book2>
<name>Java</name>
<author>老朱</author>
<publishDate>2002-6-6</publishDate>
<price>35.0</price>
</book2>
</book>
</bookList>
这样就对了。
在JDOM里这样写。
Element element = new Element( "publish" );
element.setText( "南京大学" );
doc.getRootElement( "bookList" ).getChild( "book2" ).addaddContent( element );
OK!!!