Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何更改XML文件中的特定行?_Java_Text_Special Characters - Fatal编程技术网

Java 如何更改XML文件中的特定行?

Java 如何更改XML文件中的特定行?,java,text,special-characters,Java,Text,Special Characters,我有一个很长的配置文件(不是我自己的),我想更改其中的一行 例如,我需要将ServerPort值更改为12345,但代码不知道实际的数字..(26900) 这是配置文件的一小部分: <?xml version="1.0"?> <ServerSettings> <property name="ServerPort" value="26900"/> <!-- Port you want the serv

我有一个很长的配置文件(不是我自己的),我想更改其中的一行

例如,我需要将ServerPort值更改为12345,但代码不知道实际的数字..(26900)

这是配置文件的一小部分:

<?xml version="1.0"?>
<ServerSettings>
  <property name="ServerPort"               value="26900"/>             <!-- Port you want the server to listen on. -->
  <property name="ServerIsPublic"           value="true"/>              <!-- Should this server register to master server -->
  <property name="ServerName"               value="My Game Host"/>      <!-- Whatever you want the name to be. -->
  <property name="ServerPassword"           value=""/>                  <!-- Password to gain entry to the server -->
  <property name="ServerMaxPlayerCount"     value="8"/>                 <!-- Maximum Concurrent Players -->
  <property name="ServerDescription"        value="A 7 Days to Die server"/> <!-- Whatever you want the description to be. -->
  <property name="ServerWebsiteURL"         value=""/>                  <!-- Website URL for the server -->

在这种特定情况下,您需要XML解析器。 无法将XML解析与常规文件修改进行比较

我正在使用jdom进行xml解析。您可以从web下载这个jdom.jar

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class Sample1 {
    public static void main(String[] args) throws IOException, JDOMException {

        File file = new File("c:\\file.xml");
        Document doc = new SAXBuilder().build(file);

        Element rootElement = doc.getRootElement();

        for (Object child : rootElement.getChildren("property")) {
            Element el = (Element) child;
            if (el.getAttributeValue("name").equalsIgnoreCase("ServerPort")) {
                el.setAttribute("value", "12345");
            }
        }

        XMLOutputter xmlOutput = new XMLOutputter();

        xmlOutput.setFormat(Format.getPrettyFormat());
        xmlOutput.output(doc, new FileWriter("c:\\file.xml"));
    }

}

在这种特定情况下,您需要XML解析器。 无法将XML解析与常规文件修改进行比较

我正在使用jdom进行xml解析。您可以从web下载这个jdom.jar

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class Sample1 {
    public static void main(String[] args) throws IOException, JDOMException {

        File file = new File("c:\\file.xml");
        Document doc = new SAXBuilder().build(file);

        Element rootElement = doc.getRootElement();

        for (Object child : rootElement.getChildren("property")) {
            Element el = (Element) child;
            if (el.getAttributeValue("name").equalsIgnoreCase("ServerPort")) {
                el.setAttribute("value", "12345");
            }
        }

        XMLOutputter xmlOutput = new XMLOutputter();

        xmlOutput.setFormat(Format.getPrettyFormat());
        xmlOutput.output(doc, new FileWriter("c:\\file.xml"));
    }

}

您提到的文件是一个xml文件。只需使用xml解析器+生成器,就可以更轻松地读取和更新字段。

您提到的文件是xml文件。只需使用xml解析器+生成器,读取和更新字段就会容易得多。

您可以使用正则表达式查找和替换端口号:

要搜索的模式:

(<property name="ServerPort"\s+?value=)"\d+?"
12345是一个示例端口

String fileContent = // open, read file.  Either line by line or the entirety
fileContent = fileContent.replaceAll("(<property name=\"ServerPort\"\\s+?value=)\"\\d+?\"", "$1\"12345\"");
// write fileContent back to disk.
String fileContent=//打开,读取文件。逐行或全部

fileContent=fileContent.replaceAll((您可以使用正则表达式查找并替换端口号:

要搜索的模式:

(<property name="ServerPort"\s+?value=)"\d+?"
12345是一个示例端口

String fileContent = // open, read file.  Either line by line or the entirety
fileContent = fileContent.replaceAll("(<property name=\"ServerPort\"\\s+?value=)\"\\d+?\"", "$1\"12345\"");
// write fileContent back to disk.
String fileContent=//打开,读取文件。逐行或全部

fileContent=fileContent.replaceAll((简单的答案是使用XML解析器并更改所需的属性值。要搜索节点,请使用XPath。

让我们看一下您的XML文件:

xmlfile.xml

<ServerSettings>
  <property name="ServerPort"               value="26900"/>             <!-- Port you want the server to listen on. -->
  <property name="ServerIsPublic"           value="true"/>              <!-- Should this server register to master server -->
  <property name="ServerName"               value="My Game Host"/>      <!-- Whatever you want the name to be. -->
  <property name="ServerPassword"           value=""/>                  <!-- Password to gain entry to the server -->
  <property name="ServerMaxPlayerCount"     value="8"/>                 <!-- Maximum Concurrent Players -->
  <property name="ServerDescription"        value="A 7 Days to Die server"/> <!-- Whatever you want the description to be. -->
  <property name="ServerWebsiteURL"         value=""/>                  <!-- Website URL for the server -->
</ServerSettings>
将从26900更改为8080。

如果您的文件非常大(比如几十兆字节),最好使用SAX解析器或尝试使用正则表达式搜索它。如果是值几千字节的正则设置文件,则上面的代码最简单。

简单的答案是使用XML解析器并更改所需的属性值。要搜索节点,请使用XPath。

让我们看一下您的XML文件:

xmlfile.xml

<ServerSettings>
  <property name="ServerPort"               value="26900"/>             <!-- Port you want the server to listen on. -->
  <property name="ServerIsPublic"           value="true"/>              <!-- Should this server register to master server -->
  <property name="ServerName"               value="My Game Host"/>      <!-- Whatever you want the name to be. -->
  <property name="ServerPassword"           value=""/>                  <!-- Password to gain entry to the server -->
  <property name="ServerMaxPlayerCount"     value="8"/>                 <!-- Maximum Concurrent Players -->
  <property name="ServerDescription"        value="A 7 Days to Die server"/> <!-- Whatever you want the description to be. -->
  <property name="ServerWebsiteURL"         value=""/>                  <!-- Website URL for the server -->
</ServerSettings>
将从26900更改为8080。


如果你的文件真的很大(比如几十兆字节)最好使用SAX解析器,或者尝试使用正则表达式搜索它。如果它是值几千字节的正则设置文件,那么上面的代码是最简单的。

到目前为止,你有没有尝试过任何东西?如果有,就用你遇到的问题发布它。这很难解释。因为我尝试用普通的方式替换它,但你无法做到这一点:引用和其他东西…你有没有检查过xml解析器是什么..以及它们在这种情况下如何帮助你?太好了!!你发现了什么?为什么你不能用它来解决这个问题?因为它不起作用--到目前为止你有没有尝试过什么?如果有,就用你遇到的问题发布它。这很难解释。因为我试图用正常的方式替换它,但你无法得到:对于引用和其他内容…你是否碰巧检查了什么是xml解析器…以及它们在这种情况下如何帮助你?太好了!!你发现了什么?为什么你不能用它解决这个问题?因为它不起作用--我需要一个不使用下载的库的代码检查此链接-我需要一个不使用下载的库的代码检查此链接-我已经用问题的正则表达式部分编辑了答案。我不确定您是如何从磁盘读取文件的,但假设您已经以某种方式这样做了。这并没有改变值:/嗯,如果您需要帮助调试代码,您将不得不发布它。我用问题的正则表达式部分编辑了答案。我不确定如何编辑您正在从磁盘读取您的文件,但假定您已经以某种方式完成了读取。它不会更改值:/n好吧,如果您想帮助调试代码,您将不得不发布它。仍然没有任何更改。您忘记了将更改的XML存回。Modified post(查看下面的注释”//save changed XML back),它可以工作。仍然什么也不做:/Like no errors,no exceptions,no clue?您是否尝试过逐行跟踪它,并观察到底出了什么问题?因为在发布之前,我用xmlfile.xml使用该代码尝试了该程序,并且它在我的PC.EDITED上工作。您尝试过跟踪它吗?它找到该文件了吗?它成功解析了吗?它是否找到了所需的节点及其属性?它是否更改了它的值?使用我的帖子中的xmlfile.xml作为单独的命令行程序进行尝试,并在一行之后跟踪它。这是读取/修改xml文件的标准方法,包含的所有类都来自
javax.xml
包(来自
org.w3c.dom
package的节点和文档)。仍然没有任何更改,忘记将更改的XML保存回。修改后的帖子(查看下面的注释”//Saving changed XML back),它可以工作。仍然什么也不做:/Like no errors,no exceptions,no clue?您是否尝试过逐行跟踪它,并观察到底出了什么问题?因为在发布之前,我用xmlfile.xml使用该代码尝试了该程序,并且它在我的PC.EDITED上工作。您尝试过跟踪它吗?它找到该文件了吗?它成功解析了吗?它是否找到了所需的节点及其属性?它是否更改了它的值?使用我的帖子中的xmlfile.xml作为单独的命令行程序进行尝试,并在一行之后跟踪它。这是读取/修改xml文件的标准方法,包含的所有类都来自
javax.xml
包(节点和文档来自
org.w3c.dom
package)。