views:

404

answers:

1

Hi,

I created an application which reads an xml document from url address. Lately the logic has been modified and now instead of receiving the url address of the XML document I receive the document content itself so I have to modify the following method:

public List<Product> getProducts(String content){
    List<Product> products = new ArrayList<Product>();
    XMLReader xr;
    try {
        xr = sp.getXMLReader();
        ProductsXMLHandler productsXmlHandler = new ProductsXMLHandler();
        xr.setContentHandler(productsXmlHandler);
        xr.parse(content);

        products = productsXmlHandler.getProducts();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return products;
}

}

As mentioned the variable String content is not an url but an XML document so I have to pass this content to XMLReader.

Currently the line xr.parse(content);

doesn't work correctly as the value 'content' is not an url anymore.

Does anyone know how should the method be modified in order to parse the XML content instead of trying to parse the content from the given url?

Thank you!

+2  A: 

in case it is the org.xml.sax.XMLReader:

InputSource is = new InputSource(new StringReader(content)); 
xr.parse(is);
Bozho