由于命名空间问题,XML 没有被解析,删除命名空间时工作

时间:2021-04-14 20:42:13

标签: java xml

我有一个像这样的 XML...

    <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">
          <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>

当我使用下面的代码解析这个 XML 时,我没有得到任何东西,就像 XML 被忽略一样,如果我删除更改 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>

并使用相同的代码进行解析,它工作正常。

这是代码...

    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));

简而言之,我想访问 ID 为“regular.Command-Nest.type1/2/3/4”的“CustType”标签并删除所有标签。

建议我如何修复它或分享一些文档链接...

0 个答案:

没有答案