DCMTK: Parsing XML files/strings to an XMLNode structure and Rendering XMLNode's to files/string.

DCMTK Version 3.6.9 OFFIS DICOM Toolkit

Loading...
Searching...
No Matches
Functions
Parsing XML files/strings to an XMLNode structure and Rendering XMLNode's to files/string.
The XML parser

## Functions
static XMLNode | XMLNode::parseString (XMLCSTR lpXMLString, XMLCSTR tag=NULL, XMLResults *pResults=NULL)
| Parse an XML string and return the root of a XMLNode tree representing the string.
static XMLNode | XMLNode::parseFile (XMLCSTR filename, XMLCSTR tag=NULL, XMLResults *pResults=NULL)
| Parse an XML file and return the root of a XMLNode tree representing the file.
static XMLNode | XMLNode::openFileHelper (XMLCSTR filename, XMLCSTR tag=NULL)
| This function is currently disabled but might be enabled in the future in a modified version.
static XMLCSTR | XMLNode::getError (XMLError error)
| This gives you a user-friendly explanation of the parsing error.
XMLSTR | XMLNode::createXMLString (int nFormat=1, int *pnSize=NULL) const
| Create an XML string starting from the current XMLNode.
XMLError | XMLNode::writeToFile (XMLCSTR filename, const char *encoding=NULL, char nFormat=1) const
| Save the content of an xmlNode inside a file.
static char | XMLNode::setGlobalOptions (XMLCharEncoding characterEncoding=XMLNode::char_encoding_UTF8, char guessWideCharChars=1, char dropWhiteSpace=1, char removeCommentsInMiddleOfText=1)
| Sets the global options for the conversions.
static XMLCharEncoding | XMLNode::guessCharEncoding (void *buffer, int bufLen, char useXMLEncodingAttribute=1)
| Guess the character encoding of the string (ascii, utf8 or shift-JIS)

## Detailed Description

## Function Documentation

## ◆ createXMLString()

XMLSTR XMLNode::createXMLString | ( | int | nFormat = 1,
|  | int * | pnSize = NULL ) const

Create an XML string starting from the current XMLNode.

The returned string should be free'd using the "freeXMLString" function.

If nFormat==0, no formatting is required otherwise this returns an user friendly XML string from a given element with appropriate white spaces and carriage returns. if pnSize is given it returns the size in character of the string.

## ◆ guessCharEncoding()

static XMLCharEncoding XMLNode::guessCharEncoding | ( | void * | buffer,
|  | int | bufLen,
|  | char | useXMLEncodingAttribute = 1 )
| static

Guess the character encoding of the string (ascii, utf8 or shift-JIS)

The "guessCharEncoding" function try to guess the character encoding. You most-probably will never have to use this function. It then returns the appropriate value of the global parameter "characterEncoding" described in the XMLNode::setGlobalOptions. The guess is based on the content of a buffer of length "bufLen" bytes that contains the first bytes (minimum 25 bytes; 200 bytes is a good value) of the file to be parsed. The XMLNode::openFileHelper function is using this function to automatically compute the value of the "characterEncoding" global parameter. There are several heuristics used to do the guess. One of the heuristic is based on the "encoding" attribute. The original XML specifications forbids to use this attribute to do the guess but you can still use it if you set "useXMLEncodingAttribute" to 1 (this is the default behavior and the behavior of most parsers). If an inconsistency in the encoding is detected, then the return value is "0".

## ◆ parseFile()

static XMLNode XMLNode::parseFile | ( | XMLCSTR | filename,
|  | XMLCSTR | tag = NULL,
|  | XMLResults * | pResults = NULL )
| static

Parse an XML file and return the root of a XMLNode tree representing the file.

The "parseFile" function parse an XML file and return the root of a XMLNode tree. The "opposite" of this function is the function "writeToFile" that re-creates an XML file from an XMLNode tree. If the XML document is corrupted, the "parseFile" method will initialize the "pResults" variable with some information that can be used to trace the error. If you still want to parse the file, you can use the APPROXIMATE_PARSING option as explained inside the note at the beginning of the "xmlParser.cpp" file.

Parameters

  filename | the path to the XML file to parse
  tag | the name of the first tag inside the XML file. If the tag parameter is omitted, this function returns a node that represents the head of the xml document including the declaration term (<? ... ?>).
  pResults | a pointer to a XMLResults variable that will contain some information that can be used to trace the XML parsing error. You can have a user-friendly explanation of the parsing error with the "getError" function.

References XMLNode(), and openFileHelper().

## ◆ parseString()

static XMLNode XMLNode::parseString | ( | XMLCSTR | lpXMLString,
|  | XMLCSTR | tag = NULL,
|  | XMLResults * | pResults = NULL )
| static

Parse an XML string and return the root of a XMLNode tree representing the string.

The "parseString" function parse an XML string and return the root of a XMLNode tree. The "opposite" of this function is the function "createXMLString" that re-creates an XML string from an XMLNode tree. If the XML document is corrupted, the "parseString" method will initialize the "pResults" variable with some information that can be used to trace the error. If you still want to parse the file, you can use the APPROXIMATE_PARSING option as explained inside the note at the beginning of the "xmlParser.cpp" file.

Parameters

  lpXMLString | the XML string to parse
  tag | the name of the first tag inside the XML file. If the tag parameter is omitted, this function returns a node that represents the head of the xml document including the declaration term (<? ... ?>).
  pResults | a pointer to a XMLResults variable that will contain some information that can be used to trace the XML parsing error. You can have a user-friendly explanation of the parsing error with the "getError" function.

References XMLNode().

## ◆ setGlobalOptions()

static char XMLNode::setGlobalOptions | ( | XMLCharEncoding | characterEncoding = XMLNode::char_encoding_UTF8,
|  | char | guessWideCharChars = 1,
|  | char | dropWhiteSpace = 1,
|  | char | removeCommentsInMiddleOfText = 1 )
| static

Sets the global options for the conversions.

The "setGlobalOptions" function allows you to change four global parameters that affect string & file parsing. First of all, you most-probably will never have to change these 3 global parameters.

Parameters

  guessWideCharChars | If "guessWideCharChars"=1 and if this library is compiled in WideChar mode, then the XMLNode::parseFile and XMLNode::openFileHelper functions will test if the file contains ASCII characters. If this is the case, then the file will be loaded and converted in memory to WideChar before being parsed. If 0, no conversion will be performed.
  guessWideCharChars | If "guessWideCharChars"=1 and if this library is compiled in ASCII/UTF8/char* mode, then the XMLNode::parseFile and XMLNode::openFileHelper functions will test if the file contains WideChar characters. If this is the case, then the file will be loaded and converted in memory to ASCII/UTF8/char* before being parsed. If 0, no conversion will be performed.
  characterEncoding | This parameter is only meaningful when compiling in char* mode (multibyte character mode). In wchar_t* (wide char mode), this parameter is ignored. This parameter should be one of the three currently recognized encodings: XMLNode::encoding_UTF8, XMLNode::encoding_ascii, XMLNode::encoding_ShiftJIS.
  dropWhiteSpace | In most situations, text fields containing only white spaces (and carriage returns) are useless. Even more, these "empty" text fields are annoying because they increase the complexity of the user's code for parsing. So, 99% of the time, it's better to drop the "empty" text fields. However The XML specification indicates that no white spaces should be lost when parsing the file. So to be perfectly XML-compliant, you should set dropWhiteSpace=0. A note of caution: if you set "dropWhiteSpace=0", the parser will be slower and your code will be more complex.
  removeCommentsInMiddleOfText | To explain this parameter, let's consider this code: XMLNode x=XMLNode::parseString("<a>foo<!-- hello -->bar<!DOCTYPE world >chu</a>","a"); XMLNode::parseString static XMLNode parseString(XMLCSTR lpXMLString, XMLCSTR tag=NULL, XMLResults *pResults=NULL) Parse an XML string and return the root of a XMLNode tree representing the string. XMLNode::XMLNode XMLNode(struct XMLNodeDataTag *pParent, XMLSTR lpszName, char isDecl) Constructors are protected, so use instead one of: XMLNode::parseString, XMLNode::parseFile,... If removeCommentsInMiddleOfText=0, then we will have: x.getText(0) -> "foo" x.getText(1) -> "bar" x.getText(2) -> "chu" x.getClear(0) --> "<!-- hello -->" x.getClear(1) --> "<!DOCTYPE world >" XMLNode::getText XMLCSTR getText(int i=0) const return ith text field XMLNode::getClear XMLClear getClear(int i=0) const return ith clear field (comments) If removeCommentsInMiddleOfText=1, then we will have: x.getText(0) -> "foobar" x.getText(1) -> "chu" x.getClear(0) --> "<!DOCTYPE world >"

Returns
  "0" when there are no errors. If you try to set an unrecognized encoding then the return value will be "1" to signal an error.

Note
  Sometime, it's useful to set "guessWideCharChars=0" to disable any conversion because the test to detect the file-type (ASCII/UTF8/char* or WideChar) may fail (rarely).

## ◆ writeToFile()

XMLError XMLNode::writeToFile | ( | XMLCSTR | filename,
|  | const char * | encoding = NULL,
|  | char | nFormat = 1 ) const

Save the content of an xmlNode inside a file.

If nFormat==0, no formatting is required otherwise this returns an user friendly XML string from a given element with appropriate white spaces and carriage returns. If the global parameter "characterEncoding==encoding_UTF8", then the "encoding" parameter is ignored and always set to "utf-8". If the global parameter "characterEncoding==encoding_ShiftJIS", then the "encoding" parameter is ignored and always set to "SHIFT-JIS". If "_XMLWIDECHAR=1", then the "encoding" parameter is ignored and always set to "utf-16". If no "encoding" parameter is given the "ISO-8859-1" encoding is used.

Generated on Sat Mar 1 2025 for DCMTK Version 3.6.9 by Doxygen 1.13.2