2

I have an XML which looks like this...

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

When I am parsing this XML using the below code I am getting not getting anything, Its like XML is being ignored, how ever if I remove change XML to this... (I have shorten the first tag here...and it works like a charm...

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

and using the same code to parse, it is working fine.

This is the code...

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

In a nutshell I want to reach the "CustType" tag for which ID is LIKE "regular.Command-Nest.type1/2/3/4" and remove all of them.

Suggest how can I fix it or share some link to documentation...

SharadxDutta
  • 1,058
  • 8
  • 21

0 Answers0