我的任务是解析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>
我正在尝试更改Deployment标记内的“名称”属性的值。我使用以下代码进行此操作:
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");