2

I am trying to parse the XML and get to a node and delete is using the below 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));

Below is the XML that I am parsing...but the problem is that I am not able to parse it because of the TrustFrameworkPolicy since it is long and has a lot of data...

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

BUT when I am testing by modifying the XML just to see if my java code is correct I am editing TrustFrameworkPolicy like this...IT IS WORKING FINE, I am able to parse and do my work

<TrustFrameworkPolicy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <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>

OUTPUT (with updated TrustFrameworkPolicy tag)

<TrustFrameworkPolicy xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <BasePolicy>
    <TenantId>egenginfob2cint.onmicrosoft.com</TenantId>
    <PolicyId>B2C_1A_User_TrustFrameworkExtensions_MultiTenant</PolicyId>
  </BasePolicy>
  
  <BuildingBlocks>
    <ClaimsSchema>
      <ClaimType Id="custom.rule1">
        <DataType>string</DataType>
      </ClaimType>
      <ClaimType Id="custom.rule2">
        <DataType>string</DataType>
      </ClaimType>
      
      
      
      
    </ClaimsSchema>
  </BuildingBlocks>
</TrustFrameworkPolicy>

Can someone tell me what is happening and guide me how to get through this...or share a link that I can refer to.

horizon
  • 453
  • 2
  • 12
  • Can you add the error you get whilep arsing? – M. Deinum Apr 15 '21 at 06:45
  • Modifications have limited benefit for insight. Which wellformedness/validity checkers did you try with which results on the original XML? – guidot Apr 15 '21 at 06:50
  • Hello @M.Deinum I am not getting any error, what I am trying to do is....Take an XML that I have, delete al the that has string "id = regular.Command-Nest." print the new updated XML with removed tags it in a new file on my local machine. So when I am running my code, (with updated ) I am getting correct XML output with data removed....but with original which is long...I am getting same file as output with nothing changed – horizon Apr 15 '21 at 06:51
  • @guidot Hello, can you please elaborate sir? – horizon Apr 15 '21 at 06:54
  • @guidot I have updated the question with the output I am getting when I am modifying TrustFrameworkPolicy TAG – horizon Apr 15 '21 at 06:57
  • @M.Deinum Sir I have upadted the question, you can see the output that I am getting when I am updating TrustFrameworkPolicy tag... – horizon Apr 15 '21 at 06:58
  • remove xmlns attribute from TrustFrameworkPolicy tag from original long data and try. – Bhargav Patel Apr 15 '21 at 07:00
  • @BhargavPatel Let me try and update you – horizon Apr 15 '21 at 07:01
  • The only difference I see is in the header. So I'm still not sure what you want to do or expect? – M. Deinum Apr 15 '21 at 07:02
  • 1
    @BhargavPatel I just removed "xmlns="http://schemas.microsoft.com/online/cpim/schemas/2013/06"" and it worked, why is this happening? – horizon Apr 15 '21 at 07:02
  • @M.Deinum That original header tag is causing trouble for me to parse the XML further down. – horizon Apr 15 '21 at 07:05
  • 1
    You removed the root namespace which contains the tag. With that your xpath expression won't match (because it is in a namespace), with the removal of that it matches because there is no namespace. To parse the first one you need to setup XPath with the proper namespace(s) to make it match. – M. Deinum Apr 15 '21 at 07:06
  • @M.Deinum you say xmlns="schemas.microsoft.com/online/cpim/schemas/2013/06" this is my main root namespace? Can you share a link or tell me how can I setup Xpath with proper namespace? – horizon Apr 15 '21 at 07:12
  • @M.Deinum said correctly. To parse the first one you need to set up XPath with the proper namespace(s) to make it match. The second option is you just need to find xmlns="schemas.microsoft.com/online/cpim/schemas/2013/06" and replace it with empty string(""). – Bhargav Patel Apr 15 '21 at 07:30
  • @BhargavPatel Can you share a link from where I can see how to setup Xpath with proper namespace? – horizon Apr 15 '21 at 07:34
  • https://www.kdgregory.com/index.php?page=xml.xpath – Bhargav Patel Apr 15 '21 at 07:39

1 Answers1

1

You can't ignore namespaces, they are fundamental. If your XML elements are in a namespace, you need to account for this in your XPath expressions.

Please search for "XPath default namespace". There are over 1800 results, showing what a common problem this is. Read a few of the high-scoring answers to get an understanding of the issue.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164