我想在XML文件中添加新内容,而不删除原始内容。我怎样才能做到这一点。
原始XML:
<collection>
<data>
<type>CHD</type>
<values>
<entry key="LOGICALCARDID">0000000</entry>
<entry key="PRIMARYKEYVALUE">001490499026060000000</entry>
<entry key="PRIMARYKEYOFFSET">53</entry>
</values></data>
<data>
<type>RECORD</type>
<values>
<entry key="LOGICALCARDID">0000000</entry>
<entry key="MAP_DP_EMV_SR">DDA_411</entry>
<entry key="MAP_SK_SVC_PERSO_SR">9864591</entry>
<entry key="MAP_DP_GRAPH_SR">GRAPH_PROFILE_1</entry>
</values></data>
<data>
<type>LOTPACK</type>
<values>
<entry key="LOT_PACK_GROUP">1</entry>
<entry key="GROUP_ID">GRP001</entry>
<entry key="GROUP_INDEX">1</entry>
<entry key="GROUP_QTY">3</entry>
<entry key="LOTPACKTYPE">64</entry>
</values></data>
</collection>
爪哇:
public class program {
/**
* @param args
*/
public static void main(String[] args) {
try {
String xmlFile = "order.xml";
String xmlFile2 = "order2.xml";
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(xmlFile);
Node LOGICALCARDID = doc.getElementsByTagName("entry").item(0);
Node PRIMARYKEYVALUE = doc.getElementsByTagName("entry").item(1);
Node LOGICALCARDID1 = doc.getElementsByTagName("entry").item(3);
for(int i = 1; i < 20; i++){
String number = Integer.toString(i);
int length = number.length();
if(length == 1){
String digit = "000000"+number;
LOGICALCARDID.setTextContent(digit);
PRIMARYKEYVALUE.setTextContent("00149049902606"+digit);
LOGICALCARDID1.setTextContent(digit);
}else if (length == 2){
String digit = "00000"+number;
LOGICALCARDID.setTextContent(digit);
PRIMARYKEYVALUE.setTextContent("00149049902606"+digit);
LOGICALCARDID1.setTextContent(digit);
}else if (length == 3){
String digit = "0000"+number;
LOGICALCARDID.setTextContent(digit);
PRIMARYKEYVALUE.setTextContent("00149049902606"+digit);
LOGICALCARDID1.setTextContent(digit);
}else if (length == 4){
String digit = "000"+number;
LOGICALCARDID.setTextContent(digit);
PRIMARYKEYVALUE.setTextContent("00149049902606"+digit);
LOGICALCARDID1.setTextContent(digit);
}
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("order2.xml"));
transformer.transform(source, result);
}catch (SAXParseException err) {
System.out.println("** Parsing error" + ", line "
+ err.getLineNumber() + ", uri " + err.getSystemId());
System.out.println(" " + err.getMessage());
} catch (SAXException e) {
Exception x = e.getException();
((x == null) ? e : x).printStackTrace();
} catch (Throwable t) {
t.printStackTrace();
}
}
预期的XML:
<collection>
<data>
<type>CHD</type>
<values>
<entry key="LOGICALCARDID">0000000</entry>
<entry key="PRIMARYKEYVALUE">001490499026060000000</entry>
<entry key="PRIMARYKEYOFFSET">53</entry>
</values></data>
<data>
<type>RECORD</type>
<values>
<entry key="LOGICALCARDID">0000000</entry>
<entry key="MAP_DP_EMV_SR">DDA_411</entry>
<entry key="MAP_SK_SVC_PERSO_SR">9864591</entry>
<entry key="MAP_DP_GRAPH_SR">GRAPH_PROFILE_1</entry>
</values></data>
<data>
<type>LOTPACK</type>
<values>
<entry key="LOT_PACK_GROUP">1</entry>
<entry key="GROUP_ID">GRP001</entry>
<entry key="GROUP_INDEX">1</entry>
<entry key="GROUP_QTY">3</entry>
<entry key="LOTPACKTYPE">64</entry>
</values></data>
</collection>
<collection>
<data>
<type>CHD</type>
<values>
<entry key="LOGICALCARDID">0000001</entry>
<entry key="PRIMARYKEYVALUE">001490499026060000001</entry>
<entry key="PRIMARYKEYOFFSET">53</entry>
</values></data>
<data>
<type>RECORD</type>
<values>
<entry key="LOGICALCARDID">0000001</entry>
<entry key="MAP_DP_EMV_SR">DDA_411</entry>
<entry key="MAP_SK_SVC_PERSO_SR">9864591</entry>
<entry key="MAP_DP_GRAPH_SR">GRAPH_PROFILE_1</entry>
</values></data>
<data>
<type>LOTPACK</type>
<values>
<entry key="LOT_PACK_GROUP">1</entry>
<entry key="GROUP_ID">GRP001</entry>
<entry key="GROUP_INDEX">1</entry>
<entry key="GROUP_QTY">3</entry>
<entry key="LOTPACKTYPE">64</entry>
</values></data>
</collection>
直到0000019 ..但我不知道如何在不删除原始内容的情况下添加XML。有人可以帮我吗?
答案 0 :(得分:0)
1)获取根元素
Node Collection = doc.getElementsByTagName("Collection").item(0);
2)创建其他元素
Node data = doc.createElement("data");
Node type = doc.createElement("type");
Node values = doc.createElement("values");
对剩余元素重复这些
3)然后将子元素追加到其直接父元素。
Collection.appendChild(data)
data.appendChild("type");
type.appendChild("values");
对剩余的子元素重复这些