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();
}
}
}
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();
}
}
}