繁体   English   中英

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

[英]Tags dissapear when parsing XML file in java?

我的任务是解析 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、UserName 和 Password 的开始标签在不应该从文件中删除时? 我的代码做错了吗? 我试图找出我哪里出错了,但似乎无法提出解决方案。

尝试:

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

Java XML dom:防止折叠空元素

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM