.. highlightlang:: us .. index:: Overview XML, XML .. _overview-xml: Overview XML ============ Betginning with UniPlot R2010 the :ref:`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. .. us.example **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, "\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; } .. us.makeindex XML, Functions .. include:: ../ftab/XML.ftab .. seealso:: :ref:`overview-files` .. _copyright-expat: 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. .. seealso:: :ref:`functions-by-categories`, http://expat.sourceforge.net/ :sub:`id-2051559`