JWH_
JWH_

Reputation: 267

Modifying specific elements in XML file using java

I'm currently trying to work with an XML file whereby I am required to update specific data.

In this case, the .xml file stores a list of guest information and the program has to allow user to change guest's details anytime. Below is a sample of my .xml file as well as the codes that supposely update the elements.

The problem I am currently facing is that it only changes the the value of the first guest everytime(John). How do i use the "position" parameter that i passsed in to find a specific guest node? e.g position = 2 , david's name will be changed instead. Any help will be greatly appreciated!

<?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>

And the method

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

}

Upvotes: 0

Views: 97

Answers (1)

ulab
ulab

Reputation: 1089

instead use setTextContent().

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

       child.setTextContent(name) ; 

   }

And also check if the node type is Node.ELEMENT_NODE.

Check this link

Upvotes: 2

Related Questions