1

我有一个看起来像这样的 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">

因此,如果您将旧标签替换为新标签,则代码将不起作用。这是为什么?

我尝试添加完整的标签及其内容,现在删除没有发生。

4

1 回答 1

1

可以通过在 XPath 表达式中替换/*with来完成/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\">\r\n" + 
        "      <BuildModel>\r\n" + 
        "             <RestSchema>\r\n" + 
        "                    <CustType Id=\"regular.type1\">\r\n" + 
        "                          <DataType>string</DataType>\r\n" + 
        "                    </CustType>\r\n" + 
        "                    <CustType Id=\"regular.type2\">\r\n" + 
        "                          <DataType>string</DataType>\r\n" + 
        "                    </CustType>\r\n" + 
        "                    <CustType Id=\"regular.Command-Nest.type1\">\r\n" + 
        "                          <DataType>string</DataType>\r\n" + 
        "                    </CustType>\r\n" + 
        "                    <CustType Id=\"regular.Command-Nest.type2\">\r\n" + 
        "                          <DataType>string</DataType>\r\n" + 
        "                    </CustType>\r\n" + 
        "                    <CustType Id=\"regular.type3\">\r\n" + 
        "                          <DataType>string</DataType>\r\n" + 
        "                    </CustType>\r\n" + 
        "                    <CustType Id=\"regular.Command-Nest.type4\">\r\n" + 
        "                          <DataType>string</DataType>\r\n" + 
        "                    </CustType>\r\n" + 
        "             </RestSchema>\r\n" + 
        "      </BuildModel>\r\n" + 
        "</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>
于 2021-04-14T16:31:17.793 回答