function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ashok  45ashok 45 

How to read particular tag in every Node in XML string ?

here, i want to read DocID for each Allow node, i have written sample DOM to read the xml.
Is there any method to caluculate the size for nodes(here am having 3 Allow nodes,based on this want to read
each DocID, based on that want to perform some operation)

Can anyone help me in this.



 String xml= ' <?xml version="1.0" encoding="utf-16"?>
<GETmethod xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Extend />
  <Successful>true</Successful>
  
  
 <CustAllowed>
    <CustomerAllow>
      <SampleData />
      <CurrentDate>10/14/2015</currentDate>
      <DocID>296</CID>
      <DType>BP</CIDType>
      
      <Allows>
        
        <ALLOW>
          <BID />
          <sampleDesc>exist1</sampleDesc>
          <sampleID>123456</sampleID>
          <DocID>111</DocPID>          
          <State />
        </ALLOW>
        
        <ALLOW>
          <BID />
          <sampleDesc>exist1</sampleDesc>
          <sampleID>123456</sampleID>
          <DocID>222</DocPID>          
          <State />
        </ALLOW>

        <ALLOW>
          <BID />
          <sampleDesc>exist1</sampleDesc>
          <sampleID>123454</sampleID>
          <DocID>333</DocPID>          
          <State />
        </ALLOW>
      
      </Allows>
    </CustAllow>
  </CustAllowed>
</GetMethod>';
  



       Dom.Document doc = new Dom.Document();
       doc.load(xml);
        //Retrieve the root element for this document.
        Dom.XMLNode root = doc.getRootElement();
        
        String strSuccessful= root.getChildElement('Successful', null).getText();
        
        Dom.XMLNode ycharge=root.getChildElement('CustAllowed',null).getChildElement('CustomerAllow',null);
        String strActivityDatey = ycharge.getChildElement('CurrentDate', null).getText();
        String strCIDy = ycharge.getChildElement('DocID',null).getText();
        String strCIDTypey = ycharge.getChildElement('DType',null).getText();
    
        Dom.XMLNode ycharge1=ycharge.getChildElement('Allows',null);
        
        for(Dom.XMLNode child: ycharge1.getChildElements())
        {
        
         Can anyone help here to read the each DocID in Allow nodes,here 3 nodes given in xml string
         i want to write for loop to read the DocID,. can anyone help me in this
          for(integer i=0;i<child.getAttributeCount();i++)
          {
           system.debug('DocID is +++++++ ' +child.gettext());
          }
        }
Best Answer chosen by ashok 45
pconpcon
You just need to do a child.getChildElement('DocID', null) for each child
 
String xml= '<?xml version="1.0" encoding="utf-8"?> <GetMethod xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Extend /> <Successful>true</Successful> <CustAllowed> <CustomerAllow> <SampleData /> <CurrentDate>10/14/2015</CurrentDate> <DocID>296</DocID> <DType>BP</DType> <Allows> <ALLOW> <BID /> <sampleDesc>exist1</sampleDesc> <sampleID>123456</sampleID> <DocID>111</DocID> <State /> </ALLOW> <ALLOW> <BID /> <sampleDesc>exist1</sampleDesc> <sampleID>123456</sampleID> <DocID>222</DocID> <State /> </ALLOW> <ALLOW> <BID /> <sampleDesc>exist1</sampleDesc> <sampleID>123454</sampleID> <DocID>333</DocID> <State /> </ALLOW> </Allows> </CustomerAllow> </CustAllowed> </GetMethod>';
  

Dom.Document doc = new Dom.Document();
doc.load(xml);
Dom.XMLNode root = doc.getRootElement();

String strSuccessful= root.getChildElement('Successful', null).getText();

Dom.XMLNode ycharge=root.getChildElement('CustAllowed',null).getChildElement('CustomerAllow',null);
String strActivityDatey = ycharge.getChildElement('CurrentDate', null).getText();
String strCIDy = ycharge.getChildElement('DocID',null).getText();
String strCIDTypey = ycharge.getChildElement('DType',null).getText();

Dom.XMLNode ycharge1 = ycharge.getChildElement('Allows',null);

for(Dom.XMLNode child: ycharge1.getChildElements()) {
    System.debug(System.LoggingLevel.ERROR, child.getChildElement('DocID', null).getText());
}

Also, your xml wasn't valid, so I cleaned it up.  You may want to look at running it through xmllint [1]

[1] http://xmlsoft.org/xmllint.html