LMMS
Loading...
Searching...
No Matches
juce::XmlDocument Class Reference

#include <juce_XmlDocument.h>

Public Member Functions

 XmlDocument (const String &documentText)
 XmlDocument (const File &file)
 ~XmlDocument ()
std::unique_ptr< XmlElementgetDocumentElement (bool onlyReadOuterDocumentElement=false)
std::unique_ptr< XmlElementgetDocumentElementIfTagMatches (StringRef requiredTag)
const StringgetLastParseError () const noexcept
void setInputSource (InputSource *newSource) noexcept
void setEmptyTextElementsIgnored (bool shouldBeIgnored) noexcept

Static Public Member Functions

static std::unique_ptr< XmlElementparse (const File &file)
static std::unique_ptr< XmlElementparse (const String &xmlData)

Private Member Functions

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

Private Attributes

String originalText
String::CharPointerType input { nullptr }
bool outOfData = false
bool errorOccurred = false
String lastError
String dtdText
StringArray tokenisedDTD
bool needToLoadDTD = false
bool ignoreEmptyTextElements = true
std::unique_ptr< InputSourceinputSource

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"));
if (auto mainElement = myDocument.getDocumentElement())
{
..use the element
}
else
{
String error = myDocument.getLastParseError();
}
Definition juce_File.h:45
Definition juce_String.h:53
XmlDocument(const String &documentText)
Definition juce_XmlDocument.cpp:26
int error
Definition extract.c:1038

Or you can use the helper functions for much less verbose parsing..

if (auto xml = parseXML (myXmlFile))
{
if (xml->hasTagName ("foobar"))
{
...etc
}
}
std::unique_ptr< XmlElement > parseXML(const String &textToParse)
Definition juce_XmlDocument.cpp:41
See also
XmlElement

@tags{Core}

Constructor & Destructor Documentation

◆ XmlDocument() [1/2]

juce::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]

juce::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()

juce::XmlDocument::~XmlDocument ( )

Destructor.

Member Function Documentation

◆ expandEntity()

String juce::XmlDocument::expandEntity ( const String & ent)
private

◆ expandExternalEntity()

String juce::XmlDocument::expandExternalEntity ( const String & entity)
private

◆ getDocumentElement()

std::unique_ptr< XmlElement > juce::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, or nullptr if there was an error.
See also
getLastParseError, getDocumentElementIfTagMatches

◆ getDocumentElementIfTagMatches()

std::unique_ptr< XmlElement > juce::XmlDocument::getDocumentElementIfTagMatches ( StringRef requiredTag)

Does an inexpensive check to see whether the outer element has the given tag name, and then does a full parse if it matches. If the tag is different, or the XML parse fails, this will return nullptr.

◆ getFileContents()

String juce::XmlDocument::getFileContents ( const String & filename) const
private

◆ getLastParseError()

const String & juce::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 juce::XmlDocument::getParameterEntity ( const String & entity)
private

◆ parse() [1/2]

std::unique_ptr< XmlElement > juce::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, or nullptr if there was an error.

◆ parse() [2/2]

std::unique_ptr< XmlElement > juce::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, or nullptr if there was an error.

◆ parseDocumentElement()

std::unique_ptr< XmlElement > juce::XmlDocument::parseDocumentElement ( String::CharPointerType textToParse,
bool outer )
private

◆ parseDTD()

bool juce::XmlDocument::parseDTD ( )
private

◆ parseHeader()

bool juce::XmlDocument::parseHeader ( )
private

◆ readChildElements()

void juce::XmlDocument::readChildElements ( XmlElement & parent)
private

◆ readEntity()

void juce::XmlDocument::readEntity ( String & result)
private

◆ readNextChar()

juce_wchar juce::XmlDocument::readNextChar ( )
privatenoexcept

◆ readNextElement()

XmlElement * juce::XmlDocument::readNextElement ( bool alsoParseSubElements)
private

◆ readQuotedString()

void juce::XmlDocument::readQuotedString ( String & result)
private

◆ setEmptyTextElementsIgnored()

void juce::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 ignored during parsing. If you need to catch whitespace-only text, then you should set this to false before calling the getDocumentElement() method.

◆ setInputSource()

void juce::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 juce::XmlDocument::setLastError ( const String & desc,
bool carryOn )
private

◆ skipNextWhiteSpace()

void juce::XmlDocument::skipNextWhiteSpace ( )
private

Member Data Documentation

◆ dtdText

String juce::XmlDocument::dtdText
private

◆ errorOccurred

bool juce::XmlDocument::errorOccurred = false
private

◆ ignoreEmptyTextElements

bool juce::XmlDocument::ignoreEmptyTextElements = true
private

◆ input

String::CharPointerType juce::XmlDocument::input { nullptr }
private

◆ inputSource

std::unique_ptr<InputSource> juce::XmlDocument::inputSource
private

◆ lastError

String juce::XmlDocument::lastError
private

◆ needToLoadDTD

bool juce::XmlDocument::needToLoadDTD = false
private

◆ originalText

String juce::XmlDocument::originalText
private

◆ outOfData

bool juce::XmlDocument::outOfData = false
private

◆ tokenisedDTD

StringArray juce::XmlDocument::tokenisedDTD
private

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