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
abhishek_pandey1989@yahoo.comabhishek_pandey1989@yahoo.com 

How to read String type Xml Format through Xml Stream Reader..??

I am trying to read the Xml Format in salesforce  by doing this

 

bodyxml ='<?xml version="1.0"?><Data><Result>True</Result><Password>hello</Password></Data>';

 

But what i am unable to do is how to get it iterated , in XMLStreamReader Class , I have been through the example below but it iterates on the class and i m not able to understand how is it doing with the Class "Book" as shown below ...

 

What need is to learn how Iterate through the string as defined above ..... In an XMLStreamReader Class so i can see step by step working in debug . The Code given as help by salesforce is given below . Pls have look ......

 

I hope i have desbribed it right ....

 

public class XmlStreamReaderDemo {

   // Create a class Book for processing
   public class Book 
   {
     String name;
     String author;
   }

   public Book[] parseBooks(XmlStreamReader reader) 
   {
     Book[] books = new Book[0];
     while(reader.hasNext()) {
         //  Start at the beginning of the book and make sure that it is a book
         if (reader.getEventType() == XmlTag.START_ELEMENT) {
            if ('Book' == reader.getLocalName()) {
            //  Pass the book to the parseBook method (below) 
                Book book = parseBook(reader);
                books.add(book);
            }
         }
        reader.next();
     }
    return books;
   }

   // Parse through the XML, determine the author and the characters
   Book parseBook(XmlStreamReader reader) 
   {
     Book book = new Book();
     book.author = reader.getAttributeValue(null, 'author');
     while(reader.hasNext()) {
        if (reader.getEventType() == XmlTag.END_ELEMENT) {
           break;
        } else if (reader.getEventType() == XmlTag.CHARACTERS) {
           book.name = reader.getText();
        }
        reader.next();
     }
     return book;
   }
}

 

 

There is one more problem with this code that it gets saved for the first time but after some changes of like writing a simple debug it starts giving an error ...

 

Error: Save error: Method does not exist or incorrect signature: [XMLStreamReader].getLocalName()

 

Document Dom is one method i want to understand  XMLstreamReader

 

Thank you

 

 

Vinit_KumarVinit_Kumar

Try the below code;

 

This would assing the Value 'Hello' which is under <Password> tag to String variable s.

 

 

 while(reader.hasNext()) 
        {
if(reader.getLocalName() == 'Password') 

{
     String s = reader.getText();
}
  reader.next();              
}

 

abhishek_pandey1989@yahoo.comabhishek_pandey1989@yahoo.com

I did the this but enable to save the class

public class XmlStreamReaderDemo {

  public void tt()  
  {
   XmlStreamReader reader = new  XmlStreamReader ();
   String s='<?xml version="1.0"?><Data><Result>True</Result><Password>hello</Password></Data>';
   while(reader.hasNext()) 
        {
            if(reader.getLocalName() == 'Password') 
            
            {
                  s = reader.getText();
            }
          reader.next();              
        }
  }      
}

 

Gives me the below error

 

Error: Compile Error: Method does not exist or incorrect signature: [XMLStreamReader].getText() at line 12 column 23

abhishek_pandey1989@yahoo.comabhishek_pandey1989@yahoo.com

Also tried this ...

public class XmlStreamReaderDemo {

  public void tt()  
  {
   String s='<?xml version="1.0"?><Data><Result>True</Result><Password>hello</Password></Data>';
   XmlStreamReader reader = new  XmlStreamReader (s);
   
   while(reader.hasNext()) 
        {
            if(reader.getLocalName() == 'Password')             
            {
                  s = reader.getText();
            }
          reader.next();              
        }
  }      
}

 

 

Gives me below error

 

Error: Compile Error: Constructor not defined: [XMLStreamReader].<Constructor>(String) at line 6 column 29

Vinit_KumarVinit_Kumar

Try below code :-

 

String str;
 
   String s='<?xml version="1.0"?><Data><Result>True</Result><Password>hello</Password></Data>';
   XmlStreamReader reader = new  XmlStreamReader (s);
   
 while(reader.hasNext()) 
                    {
                    
            system.debug('############4444'+reader.getEventType());
            if (reader.getEventType() == XmlTag.START_DOCUMENT)
            {
                system.debug('############333' + reader.getEventType());
            }
                if (reader.getEventType() == XmlTag.START_ELEMENT)
            {
   if(reader.getLocalName() == 'Password') 
                {
                    system.debug('############22' + reader.getLocalName());
                   
                        if (reader.getEventType() == XmlTag.END_ELEMENT) 
                        {
                            system.debug('############11');
                            break;
                        }
                        
                         
                       
                    }
                }
                            
                        if (reader.getEventType() == XmlTag.CHARACTERS) 
                        {
                            str= reader.getText();
                            system.debug('############' + str);
                        }
                        
                        
            
                        reader.next();
            }
       

 

Nishad BashaNishad Basha
Hi, Vinit_Kumar

content type as xml format using future(callout=true) in salesforce using trigger in salesforce.
given the request as string xml format and i want to get the response in xml format.can you please give me the example of above scenario