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

Detailed Description

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

The parser will expand entities that are defined in the document's DTD, but won't check the document for validity against the DTD. External DTD subsets and external parameter entities are only loaded through an input source supplied with setInputSource(). Documents whose elements are nested more than 256 levels deep, or whose entities are nested more than 40 levels deep or expand to many times the size of the document, are rejected as malformed.

e.g.

XmlDocument myDocument (File ("myfile.xml"));
if (auto mainElement = myDocument.getDocumentElement())
{
use the element
}
else
{
String error = myDocument.getLastParseError();
}
Represents a local file or directory.
Definition juce_File.h:59
The JUCE String class!
Definition juce_String.h:67
XmlDocument(const String &documentText)
Creates an XmlDocument from the xml text.

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)
Attempts to parse some XML text, returning a new XmlElement if it was valid.
See also
XmlElement

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

Public Member Functions

 XmlDocument (const String &documentText)
 Creates an XmlDocument from the xml text.
 XmlDocument (const File &file)
 Creates an XmlDocument from a file.
 ~XmlDocument ()
 Destructor.
std::unique_ptr< XmlElement > getDocumentElement (bool onlyReadOuterDocumentElement=false)
 Creates an XmlElement object to represent the main document node.
std::unique_ptr< XmlElement > 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.
const String & getLastParseError () const noexcept
 Returns the parsing error that occurred the last time getDocumentElement was called.
void setInputSource (InputSource *newSource) noexcept
 Sets an input source object to use for reading the document and any external entities that it references.
void setEmptyTextElementsIgnored (bool shouldBeIgnored) noexcept
 Sets a flag to change the treatment of empty text elements.

Static Public Member Functions

static std::unique_ptr< XmlElement > parse (const File &file)
 A handy static method that parses a file.
static std::unique_ptr< XmlElement > parse (const String &xmlData)
 A handy static method that parses some XML data.

Constructors and Destructors

◆ 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 Functions

◆ 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.

◆ 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.

◆ setInputSource()

void juce::XmlDocument::setInputSource ( InputSource * newSource)
noexcept

Sets an input source object to use for reading the document and any external entities that it references.

Supplying an input source is the only way to enable the loading of external DTD subsets and external parameter entities; a document created from a file or a string will not load them on its own. A document can name any file or URL as an external entity, so an input source used with untrusted documents should restrict what its createInputStreamFor() method will open.

The input source is also used to read the document itself if the document was constructed from a file or from an empty string, as any text passed to the constructor takes precedence over it.

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

See also
InputSource

References setInputSource().

Referenced by setInputSource().

◆ 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.

References setEmptyTextElementsIgnored().

Referenced by setEmptyTextElementsIgnored().

◆ 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.

References parse().

Referenced by parse(), and parse().

◆ 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.

References parse().