簡體   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