JUCE  v5.1.1-3-g1a0b28c73
JUCE API
XmlDocument Class Reference

Parses a text-based XML document and creates an XmlElement object from it. More...

#include <juce_XmlDocument.h>

Collaboration diagram for XmlDocument:

Public Member Functions

 XmlDocument (const String &documentText)
 Creates an XmlDocument from the xml text. More...
 
 XmlDocument (const File &file)
 Creates an XmlDocument from a file. More...
 
 ~XmlDocument ()
 Destructor. More...
 
XmlElementgetDocumentElement (bool onlyReadOuterDocumentElement=false)
 Creates an XmlElement object to represent the main document node. More...
 
const StringgetLastParseError () const noexcept
 Returns the parsing error that occurred the last time getDocumentElement was called. More...
 
void setEmptyTextElementsIgnored (bool shouldBeIgnored) noexcept
 Sets a flag to change the treatment of empty text elements. More...
 
void setInputSource (InputSource *newSource) noexcept
 Sets an input source object to use for parsing documents that reference external entities. More...
 

Static Public Member Functions

static XmlElementparse (const File &file)
 A handy static method that parses a file. More...
 
static XmlElementparse (const String &xmlData)
 A handy static method that parses some XML data. More...
 

Private Member Functions

String expandEntity (const String &)
 
String expandExternalEntity (const String &)
 
String getFileContents (const String &) const
 
String getParameterEntity (const String &)
 
XmlElementparseDocumentElement (String::CharPointerType, bool outer)
 
bool parseDTD ()
 
bool parseHeader ()
 
void readChildElements (XmlElement &)
 
void readEntity (String &)
 
juce_wchar readNextChar () noexcept
 
XmlElementreadNextElement (bool alsoParseSubElements)
 
void readQuotedString (String &)
 
void setLastError (const String &, bool carryOn)
 
void skipNextWhiteSpace ()
 

Private Attributes

String dtdText
 
bool errorOccurred
 
bool ignoreEmptyTextElements
 
String::CharPointerType input
 
ScopedPointer< InputSourceinputSource
 
String lastError
 
bool needToLoadDTD
 
String originalText
 
bool outOfData
 
StringArray tokenisedDTD
 

Detailed Description

Parses a text-based XML document and creates an XmlElement object from it.

The parser will parse DTDs to load external entities but won't check the document for validity against the DTD.

e.g.

XmlDocument myDocument (File ("myfile.xml"));
ScopedPointer<XmlElement> mainElement (myDocument.getDocumentElement());
if (mainElement == nullptr)
{
String error = myDocument.getLastParseError();
}
else
{
..use the element
}

Or you can use the static helper methods for quick parsing..

if (xml != nullptr && xml->hasTagName ("foobar"))
{
...etc
See also
XmlElement

Constructor & Destructor Documentation

◆ XmlDocument() [1/2]

XmlDocument::XmlDocument ( const String documentText)

Creates an XmlDocument from the xml text.

The text doesn't actually get parsed until the getDocumentElement() method is called.

◆ XmlDocument() [2/2]

XmlDocument::XmlDocument ( const File file)

Creates an XmlDocument from a file.

The text doesn't actually get parsed until the getDocumentElement() method is called.

◆ ~XmlDocument()

XmlDocument::~XmlDocument ( )

Destructor.

Member Function Documentation

◆ expandEntity()

String XmlDocument::expandEntity ( const String )
private

◆ expandExternalEntity()

String XmlDocument::expandExternalEntity ( const String )
private

◆ getDocumentElement()

XmlElement* XmlDocument::getDocumentElement ( bool  onlyReadOuterDocumentElement = false)

Creates an XmlElement object to represent the main document node.

This method will do the actual parsing of the text, and if there's a parse error, it may returns nullptr (and you can find out the error using the getLastParseError() method).

See also the parse() methods, which provide a shorthand way to quickly parse a file or string.

Parameters
onlyReadOuterDocumentElementif true, the parser will only read the first section of the file, and will only return the outer document element - this allows quick checking of large files to see if they contain the correct type of tag, without having to parse the entire file
Returns
a new XmlElement which the caller will need to delete, or null if there was an error.
See also
getLastParseError

◆ getFileContents()

String XmlDocument::getFileContents ( const String ) const
private

◆ getLastParseError()

const String& XmlDocument::getLastParseError ( ) const
noexcept

Returns the parsing error that occurred the last time getDocumentElement was called.

Returns
the error, or an empty string if there was no error.

◆ getParameterEntity()

String XmlDocument::getParameterEntity ( const String )
private

◆ parse() [1/2]

static XmlElement* XmlDocument::parse ( const File file)
static

A handy static method that parses a file.

This is a shortcut for creating an XmlDocument object and calling getDocumentElement() on it.

Returns
a new XmlElement which the caller will need to delete, or null if there was an error.

◆ parse() [2/2]

static XmlElement* XmlDocument::parse ( const String xmlData)
static

A handy static method that parses some XML data.

This is a shortcut for creating an XmlDocument object and calling getDocumentElement() on it.

Returns
a new XmlElement which the caller will need to delete, or null if there was an error.

◆ parseDocumentElement()

XmlElement* XmlDocument::parseDocumentElement ( String::CharPointerType  ,
bool  outer 
)
private

◆ parseDTD()

bool XmlDocument::parseDTD ( )
private

◆ parseHeader()

bool XmlDocument::parseHeader ( )
private

◆ readChildElements()

void XmlDocument::readChildElements ( XmlElement )
private

◆ readEntity()

void XmlDocument::readEntity ( String )
private

◆ readNextChar()

juce_wchar XmlDocument::readNextChar ( )
privatenoexcept

◆ readNextElement()

XmlElement* XmlDocument::readNextElement ( bool  alsoParseSubElements)
private

◆ readQuotedString()

void XmlDocument::readQuotedString ( String )
private

◆ setEmptyTextElementsIgnored()

void XmlDocument::setEmptyTextElementsIgnored ( bool  shouldBeIgnored)
noexcept

Sets a flag to change the treatment of empty text elements.

If this is true (the default state), then any text elements that contain only whitespace characters will be ingored during parsing. If you need to catch whitespace-only text, then you should set this to false before calling the getDocumentElement() method.

◆ setInputSource()

void XmlDocument::setInputSource ( InputSource newSource)
noexcept

Sets an input source object to use for parsing documents that reference external entities.

If the document has been created from a file, this probably won't be needed, but if you're parsing some text and there might be a DTD that references external files, you may need to create a custom input source that can retrieve the other files it needs.

The object that is passed-in will be deleted automatically when no longer needed.

See also
InputSource

◆ setLastError()

void XmlDocument::setLastError ( const String ,
bool  carryOn 
)
private

◆ skipNextWhiteSpace()

void XmlDocument::skipNextWhiteSpace ( )
private

Member Data Documentation

◆ dtdText

String XmlDocument::dtdText
private

◆ errorOccurred

bool XmlDocument::errorOccurred
private

◆ ignoreEmptyTextElements

bool XmlDocument::ignoreEmptyTextElements
private

◆ input

String::CharPointerType XmlDocument::input
private

◆ inputSource

ScopedPointer<InputSource> XmlDocument::inputSource
private

◆ lastError

String XmlDocument::lastError
private

◆ needToLoadDTD

bool XmlDocument::needToLoadDTD
private

◆ originalText

String XmlDocument::originalText
private

◆ outOfData

bool XmlDocument::outOfData
private

◆ tokenisedDTD

StringArray XmlDocument::tokenisedDTD
private

The documentation for this class was generated from the following file: