JDOM xml(带命名空间)文件修改

package com.amaxgs.util;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class ModifyXml {


public void doc2XmlFile(String filepath1, String filepath2,
Map<String, String> TitleBlock, Map<String, String> PageTwo,
Map<String, String> PageThree, Map<String, String> ChangeHistory)
throws Exception {
try {
File file = new File(filepath1);
SAXBuilder builder = new SAXBuilder();
Document document = (Document) builder.build(file);
String chgNumber = modifyDoc(document, TitleBlock, PageTwo,
PageThree, ChangeHistory);


// 写回XML文件
Format format = Format.getPrettyFormat();
format.setEncoding("UTF-8");
XMLOutputter output = new XMLOutputter(format);
output.output(document, new FileOutputStream(filepath2 + chgNumber
+ ".xml"));
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e.getMessage());
}
}


@SuppressWarnings("unchecked")
public String modifyDoc(Document document, Map<String, String> TitleBlock,
Map<String, String> PageTwo, Map<String, String> PageThree,
Map<String, String> ChangeHistory) {
Element root = (Element) document.getRootElement();
Namespace ns = root.getNamespace();
Element change = root.getChild("ChangeOrders", ns);
String chgNumber = change.getChild("CoverPage", ns).getChildText(
"Number", ns);
List<Element> AI = change.getChildren("AffectedItems", ns);
if (AI.size() > 0) {
change.removeChildren("AffectedItems", ns);
}


List<Element> docs = root.getChildren("Documents", ns);
if (docs.size() > 0) {
change.removeChildren("Documents", ns);
}


List<Element> parts = root.getChildren("Parts", ns);
System.out.println("parts_size : " + parts.size());
TitleBlock = new Util().getNodeValue("TitleBlock", TitleBlock);
PageTwo = new Util().getNodeValue("PageTwo", PageTwo);
PageThree = new Util().getNodeValue("PageThree", PageThree);
ChangeHistory = new Util().getNodeValue("ChangeHistory", ChangeHistory);


for (int i = 0; i < parts.size(); i++) {
Element p1 = parts.get(i).getChild("TitleBlock", ns);
if (p1 != null) {
List<Element> el = p1.getChildren();
for (Element elem : el) {
String nameOld = elem.getName();
String value = TitleBlock.get(nameOld);
System.out.println("name P1 : " + elem.getName());
if (value == null || value.equals("")) {
continue;
} else {
elem.setName(value);
}
}
}


Element p2 = parts.get(i).getChild("PageTwo", ns);
if (p2 != null) {
List<Element> el = p2.getChildren();
for (Element elem : el) {
String nameOld = elem.getName();
String value = PageTwo.get(nameOld);
System.out.println("name p2 : " + elem.getName());
if (value == null || value.equals("")) {
continue;
} else {
elem.setName(value);
}
}
}


Element p3 = parts.get(i).getChild("PageThree", ns);
if (p3 != null) {
List<Element> el = p3.getChildren();
for (Element elem : el) {
String nameOld = elem.getName();
String value = PageThree.get(nameOld);
System.out.println("name p3 : " + elem.getName());
if (value == null || value.equals("")) {
continue;
} else {
elem.setName(value);
}
}
}


Element changeHistory = parts.get(i).getChild("ChangeHistory", ns);
if (changeHistory != null) {
List<Element> el = changeHistory.getChildren();
for (Element elem : el) {
String nameOld = elem.getName();
String value = ChangeHistory.get(nameOld);
System.out
.println("name ChangeHistory : " + elem.getName());
if (value == null || value.equals("")) {
continue;
} else {
elem.setName(value);
}
}
}
}
return chgNumber;
}


public static void main(String[] args) {
Map<String, String> TitleBlock = new HashMap<String, String>();
Map<String, String> PageTwo = new HashMap<String, String>();
Map<String, String> PageThree = new HashMap<String, String>();
Map<String, String> ChangeHistory = new HashMap<String, String>();
try {
new ModifyXml().doc2XmlFile("src/agile.xml", "src/agile2.xml",
TitleBlock, PageTwo, PageThree, ChangeHistory);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值