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
Aruna06Aruna06 

regarding xmlstreamreader(simple issue)

Hi,

 

   I have changed a little bit code from xmlstreamreaderdemo which I got from google.It is not returning the value when i changed the code can anyone help me in this issue

 

here is the code that I got from google

public class XmlStreamReaderDemo {

// Create a class Book for processing      

  public class Book {   

   String name;     

   String author;   

 }

   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, deterimine the auther 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;

   }

// Test that the XML string contains specific values

        static testMethod void testBookParser() {

     XmlStreamReaderDemo demo = new XmlStreamReaderDemo();

     String str = '<books><book author="Chatty">Foo bar</book>' + '<book author="Sassy">Baz</book></books>';

     XmlStreamReader reader = new XmlStreamReader(str);     

Book[] books = demo.parseBooks(reader);

     System.debug(books.size());

     for (Book book : books) {    

    System.debug(book);  

    }   

 }

}

 

 

and the code which i edited is

 

public class XmlStreamDemo {

     public string link{get;set;}  

  public string socialnetwork{get;set;}   

    public class Network{     

String name;     

 String field;

   }

   Network[] parseNetworks(XmlStreamReader reader) {     

Network[] socialnetwork= new Network[0];     

 while(reader.hasNext()) {        

  if (reader.getEventType() == XmlTag.START_ELEMENT) {           

  if ('Network' == reader.getLocalName()) {

               Network link = parseNetwork(reader);            

     socialnetwork.add(link);            

 }        

  }       

  reader.next();     

}    

return socialnetwork;  

  }   

 Network parseNetwork(XmlStreamReader reader) {   

   Network link = new Network();    

  link.field= reader.getAttributeValue(null, 'Image');   

   while(reader.hasNext()) {     

    if (reader.getEventType() == XmlTag.END_ELEMENT) {       

     break;        

 }

else if (reader.getEventType() == XmlTag.CHARACTERS) {     

       link.name = reader.getText();       

  }      

   reader.next();     

 }     

return link;  

}  

Public void refer() {

     XmlStreamDemo demo = new XmlStreamDemo();

     String str = '<socialnetwork><link Image="Chatty">Aruna</link>' +  '<link Image="Sassy">Baz</link></socialnetwork>';     

   XmlStreamReader reader = new XmlStreamReader(str);       

   Network[] socialnetwork= demo.parseNetworks(reader);

     System.debug('======'+socialnetwork.size());

  // System.debug('==========='+socialnetwork);    

  for (Network link:socialnetwork) {  

      System.debug('-------'+link);     

}  

  }

}

 

whenever I paste the first code I'm getting the values in system.debug.....but when i tried my own code it's returning the size as '0'.can anyone help me in this issue

Best Answer chosen by Admin (Salesforce Developers) 
craigmhcraigmh

This:

 

if ('Network' == reader.getLocalName()) {

 

Should be this:

 

if ('link' == reader.getLocalName()) {

 

Since that is the name of the element.

All Answers

craigmhcraigmh

This:

 

if ('Network' == reader.getLocalName()) {

 

Should be this:

 

if ('link' == reader.getLocalName()) {

 

Since that is the name of the element.

This was selected as the best answer
Aruna06Aruna06

This works great....Thanks alot