Copyright© 2008-2022 Sitevision AB, all rights reserved.
@Requireable(value="XmlParserUtil") public interface XmlParserUtil
Example 1: parse a RSS XML string.
var xml = '<rss><channel><item><title>Hello World!</title></item></channel></rss>', parser = require('XmlParserUtil'); parser.parse('/rss/channel/item', xml, function(aElement) { // Handle item direct instead of adding to a list in order to preserve memory! out.println(aElement.getElement('title').getText() + '<br>'); });
Example 2: parse a ISO-8859-1 encoded RSS XML file stored in the file repository.
var xmlFile = require('ResourceLocatorUtil').getFileRepository().getNode('rss.xml'), parser = require('XmlParserUtil'); parser.parse('/rss/channel/item', xmlFile, 'ISO-8859-1', function(aElement) { // Handle item direct instead of adding to a list in order to preserve memory! out.println(aElement.getElement('title').getText() + '<br>'); });
Example 3: download and parse a remote RSS XML.
var requester = require('Requester'), parser = require('XmlParserUtil'), url = 'https://www.site.com/rss-feed.xml', options = { dataType: 'file' }; requester.get(url, options) .done(function(result, statusCode, headers) { if (statusCode === 200 && result) { parser.parse('/rss/channel/item', result, function(aElement) { // Handle item direct instead of adding to a list in order to preserve memory! out.println(aElement.getElement('title').getText() + '<br>'); }); } }).fail(function(message, status) { // GET failed, handle appropriately });
Important note!
In order to protected the system against processes that consumes to much memory and to avoid
OutOfMemoryExceptions the parsing process will be aborted and a RuntimeException thrown
if more than 5MB of text content is found inside a single element.
An instance of the Sitevision class implementing this interface can be obtained via Utils.getXmlParserUtil()
.
See Utils
for how to obtain an instance of the Utils
interface.
Modifier and Type | Method and Description |
---|---|
void |
parse(String aElementSelection,
Node aXmlFile,
String aCharset,
XmlElementHandler aXmlElementHandler)
Parse a file containing XML encoded with the supplied charset.
|
void |
parse(String aElementSelection,
Node aXmlFile,
XmlElementHandler aXmlElementHandler)
Parse a file containing XML encoded using UTF-8.
|
void |
parse(String aElementSelection,
String aXml,
XmlElementHandler aXmlElementHandler)
Parse a XML string.
|
void parse(String aElementSelection, String aXml, XmlElementHandler aXmlElementHandler) throws XmlParserException
aElementSelection
- the elements in the XML that should be passed to the XmlElementHandleraXml
- the UTF-8 XML stringaXmlElementHandler
- the XmlElementHandlerXmlParserException
- if an error occurs while parsing the XMLvoid parse(String aElementSelection, Node aXmlFile, XmlElementHandler aXmlElementHandler) throws RepositoryException, XmlParserException
aElementSelection
- the elements in the XML that should be passed to the XmlElementHandleraXmlFile
- the file containing XML encoded using UTF-8 (sv:file or sv:temporaryFile)aXmlElementHandler
- the XmlElementHandlerRepositoryException
- if an error occurs while accessing the fileXmlParserException
- if an error occurs while parsing the XMLvoid parse(String aElementSelection, Node aXmlFile, String aCharset, XmlElementHandler aXmlElementHandler) throws RepositoryException, XmlParserException
aElementSelection
- the elements in the XML that should be passed to the XmlElementHandleraXmlFile
- the file containing XML encoded using supplied encoding (sv:file or sv:temporaryFile)aCharset
- the character encoding of the the file content
(an IllegalArgument will be thrown if aCharset can not be resolved as a java.nio.charset.Charset)aXmlElementHandler
- the XmlElementHandlerRepositoryException
- if an error occurs while accessing the fileXmlParserException
- if an error occurs while parsing the XMLSitevision - Portal and Content Management Made Easy
Sitevision is an advanced Java enterprise portal product and a portlet container (JSR 286) that implements Java Content Repository (JSR 283).
Copyright© 2008-2022 Sitevision AB, all rights reserved.