0

元のコンテンツを削除せずに、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を追加する方法がわかりません。誰でも私を助けてもらえますか?

4

1 に答える 1

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");

残りの子要素についてこれらを繰り返します

于 2013-04-24T08:51:14.923 に答える