java - 使用java修改XML文件中的特定元素

标签 java xml io

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

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

我目前面临的问题是它每次只改变第一个客人的值(约翰)。如何使用我传入的“位置”参数来查找特定的 guest 节点?例如,位置= 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();
}

}

最佳答案

而是使用setTextContent()。

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

       child.setTextContent(name) ; 

   }

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

检查这个link

关于java - 使用java修改XML文件中的特定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36034244/

相关文章:

java - Param.getValue.get() 不起作用

xml - XSD 验证 - 元素 "xs"的 "xs:schema"未绑定(bind)错误

java - 如何将 ImageView 固定到布局的底部?

haskell 单子(monad) : IO [Double] to [IO Double]

java - 获取继承父进程 IO 的子进程的输出

java - Antlr4 - 子句的排序

java - Spring Boot:如何从返回ResponseEntity <Resource>的MicroService下载文件?

java - Android HttpClient 持久性 cookie

java - 调试时,如果我有一些编写器,我怎样才能得到它正在写入的文件名+路径?

android - 如何优化此布局 XML 文件以符合我的想法?