java - 在java中解析XML文件时标签消失?

标签 java xml dom

我的任务是解析 XML 文件。 XML 文件的格式如下:

<?xml version="1.0" encoding="UTF-8"?>
<TestConfiguration>
<Deployment name="PreUpgrade" description="First Test Target">
    <Server>
        <!-- AnalyticsURL Needs to point to the analytics page -->
        <!-- XmlpURL Needs to point to the BI Publisher page -->
        <!-- ex: <AnalyticsURL>http://servername:9704/analytics</AnalyticsURL> -->
        <!-- ex: <XmlpURL>https://servername/xmlpserver</XmlpURL> -->
        <!-- ex: <VaURL>https://servername/va</VaURL> -->
        <!-- If you are using a test certificate on the server for SSL and want to
             to bypass SSL certificate validation, set the IgnoreSSLCertErrors to true.
             This setting is strictly for Test environments only -->
        <AnalyticsURL></AnalyticsURL>
        <XmlpURL></XmlpURL>
        <VaURL></VaURL>
        <UserName></UserName>
        <Password></Password>
        <IgnoreSSLCertErrors>false</IgnoreSSLCertErrors>
    </Server>
</Deployment>
<Deployment name="PostUpgrade" description="Second Test Target">
    <Server>
        <!-- AnalyticsURL Needs to point to the analytics page -->
        <!-- XmlpURL Needs to point to the BI Publisher page -->
        <!-- ex: <AnalyticsURL>http://servername:9704/analytics</AnalyticsURL> -->
        <!-- ex: <XmlpURL>https://servername/xmlpserver</XmlpURL> -->
        <!-- ex: <VaURL>https://servername/va</VaURL> -->
        <!-- If you are using a test certificate on the server for SSL and want to
             to bypass SSL certificate validation, set the IgnoreSSLCertErrors to true.
             This setting is strictly for Test environments only -->
        <AnalyticsURL></AnalyticsURL>
        <XmlpURL></XmlpURL>
        <VaURL></VaURL>
        <UserName></UserName>
        <Password></Password>
        <IgnoreSSLCertErrors>false</IgnoreSSLCertErrors>
    </Server>
</Deployment>
</TestConfiguration>

我正在尝试更改部署标记中“名称”属性的值。我使用以下代码执行此操作:

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); 
Document doc = documentBuilder.parse("config.xml")

Node firstDeployment = doc.getElementsByTagName("Deployment").item(0); 
Node firstDeploymentName = firstDeployment.getAttributes().getNamedItem("name"); 
firstDeploymentName.setTextContext("FIRST_DEPLOYMENT"); 

Node secondDeployment = doc.getElementsByTagName("Deployment").item(1); 
Node secondDeploymentName = secondDeployment.getAttributes().getNamedItem("name"); 
secondDeploymentName.setTextContext("SECOND_DEPLOYMENT"); 

TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
Transformer transformer = transformerFactory.newTransformer(); 
DOMSource source = new DOMSource(doc); 
StreamResult result = new StreamResult(new File("config.xml")); 
transformer.transform(source, result);

config.xml 文件位于我从中调用 Java 应用程序的目录中。我面临的问题是,运行程序后,XMl 文件如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><TestConfiguration>
    <Deployment description="First Test Target" name="FIRST_DEPLOYMENT">
        <Server>
            <!-- AnalyticsURL Needs to point to the analytics page -->
            <!-- XmlpURL Needs to point to the BI Publisher page -->
            <!-- ex: <AnalyticsURL>http://servername:9704/analytics</AnalyticsURL> -->
            <!-- ex: <XmlpURL>https://servername/xmlpserver</XmlpURL> -->
            <!-- ex: <VaURL>https://servername/va</VaURL> -->
            <!-- If you are using a test certificate on the server for SSL and want to
                 to bypass SSL certificate validation, set the IgnoreSSLCertErrors to true.
                 This setting is strictly for Test environments only -->
            <AnalyticsURL/>
            <XmlpURL/>
            <VaURL/>
            <UserName/>
            <Password/>
            <IgnoreSSLCertErrors>false</IgnoreSSLCertErrors>
        </Server>
    </Deployment>
    <Deployment description="Second Test Target" name="SECOND_DEPLOYMENT">
        <Server>
            <!-- AnalyticsURL Needs to point to the analytics page -->
            <!-- XmlpURL Needs to point to the BI Publisher page -->
            <!-- ex: <AnalyticsURL>http://servername:9704/analytics</AnalyticsURL> -->
            <!-- ex: <XmlpURL>https://servername/xmlpserver</XmlpURL> -->
            <!-- ex: <VaURL>https://servername/va</VaURL> -->
            <!-- If you are using a test certificate on the server for SSL and want to
                 to bypass SSL certificate validation, set the IgnoreSSLCertErrors to true.
                 This setting is strictly for Test environments only -->
            <AnalyticsURL/>
            <XmlpURL/>
            <VaURL/>
            <UserName/>
            <Password>
            <IgnoreSSLCertErrors>false</IgnoreSSLCertErrors>
        </Server>
    </Deployment>
    </TestConfiguration>

似乎 AnalyticsURL、XmlpURL、VaURL、用户名和密码的开始标记被从文件中删除,而实际上它们不应该被删除?我的代码做错了吗?我试图找出我哪里出了问题,但似乎无法找到解决方案。

最佳答案

尝试:

transformer.setOutputProperty(OutputKeys.METHOD, "html");

( Java XML dom: prevent collapsing empty element )

关于java - 在java中解析XML文件时标签消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58859703/

相关文章:

java - 这是java教程中的错字吗?

Java:线程启动后如何继续执行?

iphone - 如何正确解析 XML 到 iPhone

javascript - 循环遍历 DOM 元素,包括 span 元素

jquery - 如何找到包含 child 最少的 parent ?

java - 在 LibGDX 中实现 Box2D 碰撞的简洁方法

java - 如何在不滚动到顶部的情况下将新项目添加到 recyclerview

xml - descendants-or-self 返回的顺序是什么,它如何与子轴一起使用?

c++ - 如何将已经写入xml文件的OpenCV Mat转换回图像?

jquery - 如何动态创建内容并将其添加到 DOM