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

Getting and opening an XML file in apex code

Hi,

 

I need to develop a class where I read in an XML file, then parse through the data, etc. This file will not be chosen manually by a user, rather I have the URL where the file is located and I need to get it from that location and open it in the code.

 

My question is, how do I get this file from the specified URL location and open it so that I can parse it in my code? I have seen many examples where the file is selected manually using apex:inputfile in the Visualforce page, but I have not been able to find anything yet where I can simply specify the location of a file and then call a method to read its contents.

 

Thanks in advance!

Prafulla PatilPrafulla Patil

Link -> http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_xml_XmlStream_reader.htm

 

 

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

 

 

1trinity1trinity

Thanks, this will help when I need to parse the XML. However my question wasn't so much on how to parse the XML, as it was about how to GET the file given a specific URL without a Visualforce page.

 

Thanks!

NisseKnudsenNisseKnudsen

Check this out:

 

 

public class ReaderFromCalloutSample {

  public void getAndParse() {

    // Get the XML document from the external server  
    
    Http http = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('http://www.cheenath.com/tutorial/sample1/build.xml');
    req.setMethod('GET');
    HttpResponse res = http.send(req);

    // Log the XML content  
    
    System.debug(res.getBody());

    // Generate the HTTP response as an XML stream  
    
    XmlStreamReader reader = res.getXmlStreamReader();

    // Read through the XML  
    
    while(reader.hasNext()) {
      System.debug('Event Type:' + reader.getEventType());
      if (reader.getEventType() == XmlTag.START_ELEMENT) {
        System.debug(reader.getLocalName());
      }
      reader.next();
    }
 
  }
}

 It's taken from the HttpResponse Class Section in the Apex Doc

 

I think this might help you with your question. When you need to GET the file from a external resource that will be perfect, I guess. On the other hand, I don't know if there might be a easiert way to read Static Resources from the platform!

 

Cheers!