有两个XML分别如下:
xml 1:
<book1>
<englishBook name = "englisg "> ABCD </englishBook>
</book1>
xml 2:
<book2>
<ChineseBook name = "englisg "> abcd </ChineseBook >
</book2>
现在想通过程序将第二个xml的内容替换为第一个内容:
用replaceChild 能实现吗?
如果不行,能替换book节点下的字节点吗?
------解决方案--------------------
自己试下不得了
------解决方案--------------------
帮忙顶。
------解决方案--------------------
用JDOM
package com.hexun.blog.infon;
import java.io.IOException;
import java.io.StringReader;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
class Test {
public static void main(String[] args) {
String xml1 = " <book1> <englishBook name = 'englisg '> ABCD </englishBook> </book1> ";
String xml2 = " <book2> <ChineseBook name = 'englisg '> abcd </ChineseBook > </book2> ";
SAXBuilder builder = new SAXBuilder();
try {
Element e1=builder.build(new StringReader(xml1))
.getRootElement().getChild( "englishBook ");
Element root2=builder.build(new StringReader(xml2)).getRootElement();
root2.removeChild( "ChineseBook ");
root2.addContent((Element)e1.clone());
System.out.println(root2.getChildText( "englishBook "));
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}