12. Overview XML¶
Betginning with UniPlot R2010 the xml_parse_string function can be used to load XML into a UniScript object. In some special cased (huge documents, paring a sub section, etc.) the Expat parser can be used.
UniScript uses a subset of Expat, Release 2.00.0. Expat is a C library written by James Clark to parse XML. Go to http://expat.sourceforge.net/ to find more about Expat.
The Expat subset used by UniScript should be sufficient to parse most XML documents.
Expat is a stream oriented XML parser. This means that the user registers handler functions, which are called during the parsing process.
Example
def startElement(hParser, name, atts)
{
hInfo = XML_GetUserData(hParser);
depthPtr = MapLookup(hInfo, "depthPtr");
fpout = MapLookup(hInfo, "fpout");
if (depthPtr > 0) {
for (i in 1:depthPtr) {
fprintf(fpout, " ");
}
}
fprintf(fpout, "<%s", name);
MapSetAt(hInfo, "name", name);
MapSetAt(hInfo, sprintf("name%d", depthPtr), name);
if (atts[1] != "") {
for (i in 1:len(atts)/2) {
fprintf(fpout, " %s=\"%s\" ", atts[i;1], atts[i;2]);
}
}
fprintf(fpout, ">\n");
depthPtr = depthPtr + 1;
MapSetAt(hInfo, "depthPtr", depthPtr);
}
def charData(hParser, data)
{
hInfo = XML_GetUserData(hParser);
fpout = MapLookup(hInfo, "fpout");
fprintf(fpout, "%s", data);
}
def endElement(hParser, name)
{
hInfo = XML_GetUserData(hParser);
depthPtr = MapLookup(hInfo, "depthPtr");
fpout = MapLookup(hInfo, "fpout");
depthPtr = depthPtr - 1;
if (depthPtr > 0) {
for (i in 1:depthPtr) {
fprintf(fpout, " ");
}
}
name = MapLookup(hInfo, sprintf("name%d", depthPtr));
fprintf(fpout, "</%s>\n", name);
MapSetAt(hInfo, "depthPtr", depthPtr);
}
def XMLFormatFile(ssFile)
{
hInfo = MapCreate();
MapSetAt(hInfo, "depthPtr", 0);
fp = fopen(ssFile, "rt")
if (!fp) return FALSE;
ss = fread(fp, "char");
fclose(fp);
fpout = fopen(ssFile + ".new", "w")
MapSetAt(hInfo, "fpout", fpout);
h = XML_ParserCreate()
XML_SetStartElementHandler(h, "startElement");
XML_SetEndElementHandler(h, "endElement");
XML_SetCharacterDataHandler(h, "charData");
XML_SetUserData(h, hInfo);
bRet = XML_Parse(h, ss);
if (bRet == 0) {
nErrCode = XML_GetErrorCode(h);
s = XML_ErrorString(nErrCode);
nLine = XML_GetCurrentLineNumber(h)
MessageBox(sprintf("Error:%d\n\n%s\n\nLine:%d", nErrCode, s, nLine));
}
XML_ParserFree(h);
MapDestroy(hInfo);
fclose(fpout);
EdCreate(ssFile + ".new")
return TRUE;
}
Functions | |
---|---|
XML_ErrorString | XML_ErrorString returns a string describing the error corresponding to code. The code should be one of the enums that can be returned from XML_GetErrorCode. |
XML_ExpatVersion | XML_ExpatVersion return the library version as a string (e.g. “expat_1.95.1”). |
XML_GetCurrentByteCount | XML_GetCurrentByteCount return the number of bytes in the current event. Returns 0 if the event is inside a reference to an internal entity. |
XML_GetCurrentByteIndex | XML_GetCurrentByteIndex |
XML_GetCurrentColumnNumber | XML_GetCurrentColumnNumber |
XML_GetCurrentLineNumber | XML_GetCurrentLineNumber returns the line number of the position. |
XML_GetErrorCode | XML_GetErrorCode returns what type of error has occurred. |
XML_GetUserData | XML_GetUserData returns the data that was set with XML_SetUserData. |
XML_Parse | XML_Parse parses a complete or a part of a document. |
XML_ParserCreate | Construct a new parser. |
XML_ParserFree | Free memory used by the parser. |
XML_SetCharacterDataHandler | Set a text handler. |
XML_SetCommentHandler | Set a handler for comments. |
XML_SetDefaultHandler | XML_SetDefaultHandler sets a handler for any characters in the document which would not otherwise be handled. |
XML_SetElementHandler | XML_SetElementHandler |
XML_SetEndElementHandler | Set handler for end (and empty) tags. |
XML_SetProcessingInstructionHandler | XML_SetProcessingInstructionHandler |
XML_SetStartElementHandler | XML_SetStartElementHandler |
XML_SetUserData | XML_SetUserData sets the user data that gets passed to handlers. |
xml_esc | xml_esc escapes special XML characters. |
xml_parse_string | xml_parse_string converts a valid XML string into a UniScript object. |
xml_save_node | xml_save_node saves a UniScript object as an XML file. |
xml_simple | xml_simple converts a valid XML string into a UniScript object. |
See also
12.1. Copyright Expat¶
Copyright (c) 1998, 1999, 2000 Thai Open Source Software Center Ltd
and Clark Cooper
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
id-2051559