使用java

时间:2016-03-16 11:22:34

标签: java xml io

我目前正在尝试使用XML文件,因此我需要更新特定数据。

在这种情况下,.xml文件存储访客信息列表,程序必须允许用户随时更改访客的详细信息。 下面是我的.xml文件示例以及假设更新元素的代码。

我目前面临的问题是它每次只更改第一位客人的价值(约翰)。我如何使用"位置"我传入的参数是为了找到特定的访客节点?例如position = 2,将改变大卫的名字。 任何帮助将不胜感激!

<?xml version="1.0" encoding="UTF-8" standalone="no"?><data>
<Guest_List>
    <Guest>
    <name>John</name>
    <address>NTU Hall 17 #01-111</address>
    <country>Singapore</country>
    <gender>Male</gender>
    <nationality>Singaporean</nationality>
    <contact>92003239</contact>
    <creditCardNo>1234567812345678</creditCardNo>
    <creditCardCSV>432</creditCardCSV>
    <creditCardExpDate>11/16</creditCardExpDate>
    <identity>U0000000I</identity>

</Guest>
<Guest>
    <name>David</name>
    <address>Jurong East St32 #02-222</address>
    <country>Singapore</country>
    <gender>Male</gender>
    <nationality>Singaporean</nationality>
    <contact>93482032</contact>
    <creditCardNo>1234567812345678</creditCardNo>
    <creditCardCSV>588</creditCardCSV>
    <creditCardExpDate>3/16</creditCardExpDate>
    <identity>U1234567I</identity>
    </Guest>  
</Guest_List>
</data>

方法

public static void updateGuestList(int position,ArrayList<Guest> g){
try{

    String filepath = "guestList.xml";
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(filepath);
    NodeList nodes1 = doc.getElementsByTagName("Guest_List");
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File(filepath));

    System.out.println("1.)Update Name:\n2.)Update Address:\n3.)"
            + "Update Country:\n4.)Update Gender:\n5.)Update Contact:\n"
            + "6.)Update Nationality:\n7.)Update Credit card number:\n"
            + "8.)Update Credit card CSV code:\n9.)Update Credit card expiry date:\n"
            + "10.)Update Identification No:");



int userInput = sc.nextInt();
switch(userInput){
case 1:
    System.out.println("1.)Enter new Name:");
    String name = sc.next();
    for(int j=0;j<nodes1.getLength();j++)
    {
        //Get the staff element by tag name directly
        Node nodes = doc.getElementsByTagName("Guest").item(j);
        //loop the staff child node
        NodeList list = nodes.getChildNodes();

        for (int i = 0; i != list.getLength(); ++i)
        {
            Node child = list.item(i);

           if (child.getNodeName().equals("name")) {

               child.getFirstChild().setNodeValue(name) ;

           }

       }
   }

    transformer.transform(source, result);
     g.get(position).setGuestName(name);
    break;
}


}

catch(Exception e){
    e.printStackTrace();
}

}

1 个答案:

答案 0 :(得分:2)

改为使用setTextContent().

   if (child.getNodeName().equals("name")) {

       child.setTextContent(name) ; 

   }

并检查节点类型是否为Node.ELEMENT_NODE

选中此link