我的任务是解析 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 - 在java中解析XML文件时标签消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58859703/