dom4j删除XML声明

williamxiao 2009-09-22 05:28:36
需求是这样的:需要将两个使用DOM4J生成的XML文件合成一个,详细如下:

有两个生成XML文件的方法,生方方法和生成的XML如下:
CreateXML1.java:

/**
* 将结果集构建成XML字符串
* @param lst 结果集
* @return XML字符串
*/
private String createXML(List lst)
{
if(lst==null||lst.size()<1)
return null;
// 获得dom4j xml文档对象
Document document = DocumentHelper.createDocument();
//创建dom4j xml根节点
Element root = document.addElement("hhsClientHealths");
//遍历list向根节点添加子元素
for (Iterator iterator = lst.iterator(); iterator.hasNext();)
{
HhsClientHealth hhsClientHealth = (HhsClientHealth) iterator.next();
Element element = root.addElement("hhsClientHealth");
element.addElement("chlthId").addText(hhsClientHealth.getChlthId());
element.addElement("clntId").addText(hhsClientHealth.getClntId());
element.addElement("clntName").addText(hhsClientHealth.getClntName());
element.addElement("blodtpId").addText(hhsClientHealth.getBlodtpId());
element.addElement("blodtpName").addText(hhsClientHealth.getBlodtpName());
element.addElement("hospId").addText(hhsClientHealth.getHospId());
element.addElement("hospName").addText(hhsClientHealth.getHospName());
element.addElement("chlthStatare").addText(hhsClientHealth.getChlthStatare().toString());
element.addElement("chlthWeight").addText(hhsClientHealth.getChlthWeight().toString());
element.addElement("chlthIllHistory").addText(hhsClientHealth.getChlthIllHistory());
element.addElement("chlthIrriHistory").addText(hhsClientHealth.getChlthIrriHistory());
}
//将XML输出到字符串
return document.asXML();
}

xml1.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<hhsClientHealths>
<hhsClientHealth>
<chlthId>1</chlthId>
<clntId>1001</clntId>
<clntName>李先生</clntName>
<blodtpId>1</blodtpId>
<blodtpName>A</blodtpName>
<hospId>1</hospId>
<hospName>附二院</hospName>
<chlthStatare>170.0</chlthStatare>
<chlthWeight>100.2</chlthWeight>
<chlthIllHistory>无</chlthIllHistory>
<chlthIrriHistory>无</chlthIrriHistory>
</hhsClientHealth>
</hhsClientHealths>


CreateXML2.java:

/**
* 将结果集构建成XML字符串
* @param lst 结果集
* @return XML字符串
*/
private String createXML(List lst)
{
if(lst==null||lst.size()<1)
return null;
// 获得dom4j xml文档对象
Document document = DocumentHelper.createDocument();
//创建dom4j xml根节点
Element root = document.addElement("bloodTypes");
//遍历list向根节点添加子元素
for (Iterator iterator = lst.iterator(); iterator.hasNext();)
{
BloodType bloodType = (BloodType) iterator.next();
Element element = root.addElement("bloodType");
element.addElement("blodtpId").addText(bloodType.getBlodtpId());
element.addElement("blodtpName").addText(bloodType.getBlodtpName());
}
//将XML输出到字符串
return document.asXML();
}

xml2.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<bloodTypes>
<bloodType>
<blodtpId>1</blodtpId>
<blodtpName>A</blodtpName>
</bloodType>
<bloodType>
<blodtpId>2</blodtpId>
<blodtpName>B</blodtpName>
</bloodType>
<bloodType>
<blodtpId>3</blodtpId>
<blodtpName>O</blodtpName>
</bloodType>
<bloodType>
<blodtpId>4</blodtpId>
<blodtpName>AB</blodtpName>
</bloodType>
</bloodTypes>


现在想要得到下面这样的XML:
want.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<hhsClientHealths>
<hhsClientHealth>
<chlthId>1</chlthId>
<clntId>1001</clntId>
<clntName>李先生</clntName>
<blodtpId>1</blodtpId>
<blodtpName>A</blodtpName>
<hospId>1</hospId>
<hospName>附二院</hospName>
<chlthStatare>170.0</chlthStatare>
<chlthWeight>100.2</chlthWeight>
<chlthIllHistory>无</chlthIllHistory>
<chlthIrriHistory>无</chlthIrriHistory>
</hhsClientHealth>
<bloodTypes>
<bloodType>
<blodtpId>1</blodtpId>
<blodtpName>A</blodtpName>
</bloodType>
<bloodType>
<blodtpId>2</blodtpId>
<blodtpName>B</blodtpName>
</bloodType>
<bloodType>
<blodtpId>3</blodtpId>
<blodtpName>O</blodtpName>
</bloodType>
<bloodType>
<blodtpId>4</blodtpId>
<blodtpName>AB</blodtpName>
</bloodType>
</bloodTypes>
</hhsClientHealths>


主要想结合这两类的方法来生成一个XML,不知各位有没有什么方法可以做.
...全文
214 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zpfsupervisor 2011-03-15
  • 打赏
  • 举报
回复
搞定了,是按照你的思路来做的.谢谢!
williamxiao 2009-09-23
  • 打赏
  • 举报
回复
搞定了,是按照你的思路来做的.谢谢!
williamxiao 2009-09-23
  • 打赏
  • 举报
回复
Sorry,这不一样,只能生成这样的文件:和需求不符
<?xml version="1.0" encoding="UTF-8"?>

<root>
<hhsClientHealths>
<hhsClientHealth>
<chlthId>1</chlthId>
<clntId>1001</clntId>
<clntName>李先生</clntName>
<blodtpId>1</blodtpId>
<blodtpName>A</blodtpName>
<hospId>1</hospId>
<hospName>附二院</hospName>
<chlthStatare>170.0</chlthStatare>
<chlthWeight>100.2</chlthWeight>
<chlthIllHistory>无</chlthIllHistory>
<chlthIrriHistory>无</chlthIrriHistory>
</hhsClientHealth>
</hhsClientHealths>
<bloodTypes>
<bloodType>
<blodtpId>1</blodtpId>
<blodtpName>A</blodtpName>
</bloodType>
<bloodType>
<blodtpId>2</blodtpId>
<blodtpName>B</blodtpName>
</bloodType>
<bloodType>
<blodtpId>3</blodtpId>
<blodtpName>O</blodtpName>
</bloodType>
<bloodType>
<blodtpId>4</blodtpId>
<blodtpName>AB</blodtpName>
</bloodType>
</bloodTypes>
</root>
goosman 2009-09-22
  • 打赏
  • 举报
回复

package org.aninggo.code.material.sessionfactory;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class T {

/**
* @param args
*/
public static void main(String[] args) {
Document d1 = null;
Document d2 = null;
Document d3 = null;
XMLWriter writer = null;
OutputFormat format = OutputFormat.createPrettyPrint();
try {
d1 = new SAXReader().read(new File("D:\\x1.xml"));
d2 = new SAXReader().read(new File("D:\\x2.xml"));
d3 = DocumentFactory.getInstance().createDocument(DocumentFactory.getInstance().createElement("root"));
d3.getRootElement().add(d1.getRootElement());
d3.getRootElement().add(d2.getRootElement());
writer = new XMLWriter(new FileWriter("D:\\x3.xml"), format);
writer.write(d3);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(writer != null) {
try {
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
writer = null;
}
}
}

}

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧