我有一个看起来像这样的 XML...
<TrustFrameworkPolicy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<BuildModel>
<RestSchema>
<CustType Id="regular.type1">
<DataType>string</DataType>
</CustType>
<CustType Id="regular.type2">
<DataType>string</DataType>
</CustType>
<CustType Id="regular.Command-Nest.type1">
<DataType>string</DataType>
</CustType>
<CustType Id="regular.Command-Nest.type2">
<DataType>string</DataType>
</CustType>
<CustType Id="regular.type3">
<DataType>string</DataType>
</CustType>
<CustType Id="regular.Command-Nest.type4">
<DataType>string</DataType>
</CustType>
</RestSchema>
</BuildModel>
</TrustFrameworkPolicy>
我正在尝试访问 ID 为 LIKE "regular.Command-Nest.type1/2/3/4" 的 "CustType" 并删除所有这些。
我用 Java 编写了一个代码来读取以下标签,但我不能。
Document document = getXmlAsDocument(pathToXml);
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("//TrustFrameworkPolicy/BuildModel/RestSchema/*");
Object result = expr.evaluate(document, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
在上面的代码中,我试图打印 RestSchema 下的所有内容,但我无法通读它。
我怎样才能以更好的方式做到这一点,我必须如上所述删除所有节点。
===== 更新 =====
第一个标签有点长...
<TrustFrameworkPolicy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.microsoft.com/online/cpim/schemas/2013/06" PolicySchemaVersion="0.3.0.0" TenantId="{Settings:Tenant}" PolicyId="B2C_1A_User_MigrationClients" PublicPolicyUri="http://{Settings:Tenant}/B2C_1A_User_MigrationClients" DeploymentMode="{Settings:DeploymentMode}" UserJourneyRecorderEndpoint="urn:journeyrecorder:applicationinsights">
因此,如果您将旧标签替换为新标签,则代码将不起作用。这是为什么?
我尝试添加完整的标签及其内容,现在删除没有发生。