我有一个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>
我正试图达到";CustType";其ID为LIKE";有规律的命令嵌套类型1/2/3/4";并将它们全部移除。
我用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">
因此,如果用新标签替换旧标签,代码将不起作用。为什么?
我尝试添加完整的标签及其内容,但现在没有删除。
这可以通过在XPath表达式中将/*
替换为/CustType[starts-with(@Id, 'regular.Command-Nest.type')]
来完成,然后更改循环以删除找到的节点。像这样:
public static void main(String[] args) throws Exception {
DocumentBuilderFactory domBuilderFactory = DocumentBuilderFactory.newInstance();
domBuilderFactory.setNamespaceAware(true);
Document document = domBuilderFactory.newDocumentBuilder().parse(new InputSource(new StringReader(XML)));
NodeList nodes = (NodeList) XPathFactory.newInstance().newXPath()
.compile("/TrustFrameworkPolicy/BuildModel/RestSchema/CustType[starts-with(@Id, 'regular.Command-Nest.type')]")
.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
nodes.item(i).getParentNode().removeChild(nodes.item(i));
}
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(document), new StreamResult(System.out));
}
static final String XML =
"<TrustFrameworkPolicy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">rn" +
" <BuildModel>rn" +
" <RestSchema>rn" +
" <CustType Id="regular.type1">rn" +
" <DataType>string</DataType>rn" +
" </CustType>rn" +
" <CustType Id="regular.type2">rn" +
" <DataType>string</DataType>rn" +
" </CustType>rn" +
" <CustType Id="regular.Command-Nest.type1">rn" +
" <DataType>string</DataType>rn" +
" </CustType>rn" +
" <CustType Id="regular.Command-Nest.type2">rn" +
" <DataType>string</DataType>rn" +
" </CustType>rn" +
" <CustType Id="regular.type3">rn" +
" <DataType>string</DataType>rn" +
" </CustType>rn" +
" <CustType Id="regular.Command-Nest.type4">rn" +
" <DataType>string</DataType>rn" +
" </CustType>rn" +
" </RestSchema>rn" +
" </BuildModel>rn" +
"</TrustFrameworkPolicy>";
输出
<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.type3">
<DataType>string</DataType>
</CustType>
</RestSchema>
</BuildModel>
</TrustFrameworkPolicy>