I am tasked with parsing an XML file. The XML file is of the following format:
<?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>
I am trying to change the value of the "name" attributes within the Deployment tags. I am doing this with the following code:
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);
The config.xml file is within the directory that I am invoking my Java application from. The issue I am facing is that after I run my program, the XMl file looks like this:
<?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>
It seems as if the opening tags for AnalyticsURL, XmlpURL, VaURL, UserName, and Password are removed from the file when they should not be? Am I doing something wrong with my code? I have tried to figure out where i've gone wrong but cant seem to come up with a solution.
Try:
transformer.setOutputProperty(OutputKeys.METHOD, "html");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.