SophiaFramework UNIVERSE 5.3 |
#include <SFXXMLDOMParser.hpp>
class SFXXMLDOMParser : public static_exception< SFCError >;
SFMTYPEDEFCLASS(SFXXMLDOMParser)
The DOM parser(SFXXMLDOMParser) converts an XML document into a tree called "the DOM tree", where any data of the XML document can be obtained flexibly regardless of its order.
Document Object Model (DOM) Specification | |
---|---|
W3C DOM level 1 recommendation: Document Object Model (DOM) Level 1 Specification ) |
Example 849. The method to parse an XML document using the DOM parser
//XML document to be parsed ( file.xml ) <fruit> <name language="english" code="ascii">apple</name> <size>XS</size> <color>red</color> </fruit> SFCError error; // error value SFXXMLDOMParser parser; // DOM parser SFXAnsiString resultString; // variable to store string obtained from XML document // parse XML document stored in file.xml if ((error = parser.Parse(SFXPath("/file.xml"))) == SFERR_NO_ERROR) { // get root node of DOM tree SFXXMLDocumentPtr root = parser.GetDocument(); // get first child node of root node(root element of XML document: "fruit" element) SFXXMLNodePtr child = root->GetFirstChild(); // get first child node of "fruit" node, i.e., "name" node) child = child->GetFirstChild(); // if name of child node is "name" if (SFXAnsiString("name").Equals(child->GetNodeName())) { // cast SFXXMLNodePtr into SFXXMLElementPtr to get attribute value of this node SFXXMLElementPtr element = static_cast<SFXXMLElementPtr>(child); // get value of "language" attribute // resultString is "english" resultString = element->GetAttribute("language"); // get Attribute node which name is "language" SFXXMLAttributePtr attr = element->GetAttributeNode("language"); // get value of this Attribute node resultString = attr->GetNodeValue(); // get all Attribute nodes SFXXMLNode::DOMNamedNodeMapPtr nodeMap = element->GetAttributes(); // get Attribute node which name is "code" attr = static_cast<SFXXMLAttributePtr>(nodeMap->GetNamedItem("code")); // get value of this Attribute node resultString = attr->GetNodeValue(); } }
Constructor/Destructor |
---|
SFXXMLDOMParser( Void ) Constructor of the SFXXMLDOMParser class.
|
~SFXXMLDOMParser( Void ) Destructor of the SFXXMLDOMParser class.
|
Public Functions | |
---|---|
Bool |
GetCreateCommentNodes( Void ) Check whether or not to create Comment nodes.
|
Bool |
GetDoIndent( Void ) Check whether or not to indent.
|
Bool |
GetDoNamespaces( Void ) Check whether or not to process namespace.
|
Bool |
GetDoSchema( Void ) Check whether or not to process the XML Schema.
|
SFXXMLDocumentPtr |
GetDocument( Void ) Get the Document node.
|
ACharConstPtr |
GetErrorInfo( Void ) Get the internal error message.
|
static SFXXMLElementPtr |
GetFirstChildElement(
SFXXMLNodeConstPtr parent
) Get the first child Element node of the specified node.
|
static SFXXMLElementPtr |
GetFirstChildElementNS(
SFXXMLNodeConstPtr parent
, SFXAnsiStringHandleConst elemNames
, SFXAnsiStringConstRef uri
, SInt32 length
) Get the first child Element node of the specified node, which matches the namespace URI and one of the local names specified.
|
Bool |
GetIgnoreAnnotations( Void ) Check whether or not to ignore annotations.
|
Bool |
GetLoadExternalDTD( Void ) Check whether or not to load the external DTD.
|
static SFXXMLElementPtr |
GetNextSiblingElement(
SFXXMLNodeConstPtr node
) Get the next sibling Element node of the specified node.
|
static SFXXMLElementPtr |
GetNextSiblingElementNS(
SFXXMLNodeConstPtr node
, SFXAnsiStringHandleConst elemNames
, SFXAnsiStringConstRef uri
, SInt32 length
) Get the next sibling Element node of the specified node, which matches the namespace URI and one of the local names specified.
|
Bool |
GetStandalone( Void ) Get the Standalone declaration of the XML document.
|
Bool |
GetValidationDTD( Void ) Check whether or not to validate the XML document with DTD.
|
Bool |
GetValidationSchema( Void ) Check whether or not to validate the XML document with XML Schema.
|
SFCError |
Parse(
SFBFileSmpConstRef source
) Parse the XML document using the DOM parser.
|
SFCError |
Parse(
SFXAnsiStringConstRef source
) Parse the XML document using the DOM parser.
|
SFCError |
Parse(
SFXPathConstRef source
) Parse the XML document using the DOM parser.
|
SFCError |
Parse(
SFXStreamReaderConstRef source
) Parse the XML document using the DOM parser.
|
Void |
Reset( Void ) Reset all the internal variables.
|
Void |
SetCreateCommentNodes(
BoolConst create
) Set whether or not to create comment nodes.
|
Void |
SetDoIndent(
BoolConst state
) Set whether or not to indent.
|
Void |
SetDoNamespaces(
BoolConst state
) Set whether or not to process namespaces.
|
Void |
SetDoSchema(
BoolConst state
) Set whether or not to process the XML Schema.
|
Void |
SetGrammar(
SFXXMLGrammar::GrammarType grammar
) Set the grammar used by this parser.
|
Void |
SetGrammar(
SFXXMLGrammar::GrammarType grammar
) Set the grammar used by this parser.
|
Void |
SetIgnoreAnnotations(
BoolConst state
) Set whether or not to ignore annotations.
|
Void |
SetIgnoreAnnotations(
BoolConst state
) Set whether or not to ignore annotations.
|
Void |
SetLoadExternalDTD(
BoolConst state
) Set whether or not to load the external DTD.
|
Void |
SetSchemaLocation(
SFXAnsiStringConstRef name
) Set the XSD file of the XML document with XML Schema.
|
Void |
SetValidationDTD(
BoolConst state
) Set whether or not to validate the XML document with DTD.
|
Void |
SetValidationSchema(
BoolConst state
) Set whether or not to validate the XML document with XML Schema.
|
Protected Functions | |
---|---|
Void |
SetGrammar(
SFXXMLGrammar::GrammarType grammar
) Set the grammar used by this parser.
|
Void |
SetGrammar(
SFXXMLGrammar::GrammarType grammar
) Set the grammar used by this parser.
|
Void |
SetIgnoreAnnotations(
BoolConst state
) Set whether or not to ignore annotations.
|
Void |
SetIgnoreAnnotations(
BoolConst state
) Set whether or not to ignore annotations.
|
[ public, explicit ] SFXXMLDOMParser(Void);
[ public ] ~SFXXMLDOMParser(Void);
[ public, const ] Bool GetCreateCommentNodes(Void);
[ public, const ] Bool GetDoIndent(Void);
[ public, const ] Bool GetDoNamespaces(Void);
[ public, const ] Bool GetDoSchema(Void);
[ public, const ] SFXXMLDocumentPtr GetDocument(Void);
The Document node is the root node of the DOM tree.
[ public, const ] ACharConstPtr GetErrorInfo(Void);
[ public, static ] SFXXMLElementPtr GetFirstChildElement( SFXXMLNodeConstPtr parent // parent node );
SFXXMLDOMParser::GetFirstChildElementNS | SFXXMLDOMParser::GetNextSiblingElement | SFXXMLDOMParser::GetNextSiblingElementNS
[ public, static ] SFXXMLElementPtr GetFirstChildElementNS( SFXXMLNodeConstPtr parent // parent node SFXAnsiStringHandleConst elemNames // handle of local names(element name):Handle type SFXAnsiStringConstRef uri // namespace URI SInt32 length // number of local names(element name) );
Note | |
---|---|
The Element node needs to match one of the element names(local name) specified with the "elemNames" argument. |
SFXXMLDOMParser::GetFirstChildElement | SFXXMLDOMParser::GetNextSiblingElement | SFXXMLDOMParser::GetNextSiblingElementNS
[ public, const ] Bool GetIgnoreAnnotations(Void);
[ public, const ] Bool GetLoadExternalDTD(Void);
[ public, static ] SFXXMLElementPtr GetNextSiblingElement( SFXXMLNodeConstPtr node // node );
SFXXMLDOMParser::GetNextSiblingElementNS | SFXXMLDOMParser::GetFirstChildElement | SFXXMLDOMParser::GetFirstChildElementNS
[ public, static ] SFXXMLElementPtr GetNextSiblingElementNS( SFXXMLNodeConstPtr node // node SFXAnsiStringHandleConst elemNames // handle of local names(element name):Handle type SFXAnsiStringConstRef uri // URI of the namespace SInt32 length // number of local names(element name) );
SFXXMLDOMParser::GetNextSiblingElement | SFXXMLDOMParser::GetFirstChildElement | SFXXMLDOMParser::GetFirstChildElementNS
[ public, const ] Bool GetStandalone(Void);
[ public, const ] Bool GetValidationDTD(Void);
[ public, const ] Bool GetValidationSchema(Void);
[ public ] SFCError Parse( SFBFileSmpConstRef source // XML file instance );
[ public ] SFCError Parse( SFXAnsiStringConstRef source // XML file name );
[ public ] SFCError Parse( SFXPathConstRef source // XML file name );
[ public ] SFCError Parse( SFXStreamReaderConstRef source // input stream );
The error codes that may occur when parsing the XML document are as follows:
The error codes that may occur when parsing the XML document with DTD are as follows:
The error codes that may occur when parsing the XML document with Schema are as follows:
Reference: SFCErrorEnum
The SFXXMLDOMParser::Parse function is to read and parse the specified XML document using the DOM parser. The argument can be an input stream, file name (of file path type), or string. The default directory of the file path is the home directory of application.
[ public ] Void Reset(Void);
[ public ] Void SetCreateCommentNodes( BoolConst create // whether or not to create comment nodes );
[ public ] Void SetGrammar( SFXXMLGrammar::GrammarType grammar // grammar type );
[ protected ] Void SetGrammar( SFXXMLGrammar::GrammarType grammar // pointer to grammar );
[ public ] Void SetIgnoreAnnotations( BoolConst state // whether or not to ignore annotations );
[ protected ] Void SetIgnoreAnnotations( BoolConst state // whether or not to ignore annotations );
[ public ] Void SetLoadExternalDTD( BoolConst state // whether or not to load the external DTD file );
[ public ] Void SetSchemaLocation( SFXAnsiStringConstRef name // local XSD file );
[ public ] Void SetValidationDTD( BoolConst state // whether or not to validate the XML document with DTD );
// XML document to be parsed ("file.xml") <?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE Mail SYSTEM "personal.dtd"> <Mail> <From>&name;</From> <To>Receiver</To> <Cc>&Email;</Cc> <Date> Thu, 7 Oct 1999 11:15:16 -0600</Date> <Subject>XML Introduction</Subject> <Body>Comments:<P align = "right">Thanks for reading<br/>this article</P> <br/> <P>Hope you enjoyed this article</P> <P>©right;</P> </Body> </Mail> // DTD file for validation ("personal.dtd") <?xml encoding="ISO-8859-1"?> <!-- @version: 10.1--> <!ELEMENT Mail (From, To, Cc?, Date?, Subject, Body)> <!ELEMENT From (#PCDATA)> <!ELEMENT To (#PCDATA)> <!ELEMENT Cc (#PCDATA)> <!ELEMENT Date (#PCDATA)> <!ELEMENT Subject (#PCDATA)> <!ELEMENT Body (#PCDATA | P | Br)*> <!ELEMENT P (#PCDATA | br)*> <!ATTLIST P align (left | right | justify) "left"> <!ELEMENT br EMPTY> <!ENTITY name "sophia"> <!ENTITY Email "&name;@s-cradle.com"> <!ENTITY copyright "Copyright (C) 2002 - 2024 Sophia Cradle Incorporated."> // parse the XML document with DTD SFXXMLDOMParser parser; SFCError error; // set grammar type to parse XML document with DTD parser.SetGrammar(SFXXMLGrammar::GRAMMAR_DTD); // set to validate XML document with DTD parser.SetValidationDTD(true); // set to load external DTD parser.SetLoadExternalDTD(true); // parse XML document with DTD using DOM parser error = parser.Parse(SFXPath("/file.xml")); if (error == SFERR_NO_ERROR) { // get root node of DOM tree SFXXMLDocumentPtr root = parser.GetDocument(); // get DocumentType node SFXXMLDocumentTypePtr documentType = root->GetDocumentType(); // get system identifier of external DTD subset // the id variable will be "personal.dtd" SFXAnsiString id = documentType->GetSystemID(); // get Entity nodes defined in DTD as DOMNamedNodeMap SFXXMLNode::DOMNamedNodeMapPtr entitymap = documentType->GetEntities(); // get value of the "name" entity // the name variable will be "sophia" SFXAnsiString name = SFXXMLEntityPtr(entitymap->GetNamedItem("name"))->GetValue(); }
[ public ] Void SetValidationSchema( BoolConst state // whether or not to validate the XML document with XML Schema );
SFXXMLDOMParser parser; // set to to create comment nodes parser.SetCreateCommentNodes(true); // set to namespaces parser.SetDoNamespaces(true); // set grammar type to parse XML document with XML Schema parser.SetGrammar(SFXXMLGrammar::GRAMMAR_SCHEMA); // set to process XML Schema parser.SetDoSchema(true); // set XSD file for XML document with XML Schema // (default directory: home directory of application) parser.SetSchemaLocation("schema.xsd"); // set to validate XML document with XML Schema parser.SetValidationSchema(true); // set to ignore annotations parser.SetIgnoreAnnotations(false); // parse XML document with XML Schema using DOM parser error = parser.Parse(SFXPath("/file.xml"));
Copyright(c) 2002 - 2024 Sophia Cradle Incorporated All Rights Reserved. |