VS:XMLSAXParseFile

From Vectorworks Developer
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

.VectorScript|VectorScript ..VS:Function Reference|Function Reference ..VS:Function_Reference_Appendix|Appendix

Description

Parse an XML file using SAX.

FUNCTION XMLSAXParseFile(
XMLHandle :LONGINT;
whichPath :INTEGER;
filename :STRING;
nodeCallback :PROCEDURE) : INTEGER;
def vs.XMLSAXParseFile(XMLHandle, whichPath, filename, nodeCallback):
    return INTEGER

Parameters

XMLHandle LONGINT
whichPath INTEGER
filename STRING
nodeCallback PROCEDURE

Example

VectorScript

PROCEDURE XMLSaxTest_Read;
CONST
	kExternalsFolder 		= 2;

	kSAXNODEType_StartDoc		= 1;
	kSAXNODEType_EndDoc		= 2;
	kSAXNODEType_StartNode		= 3;
	kSAXNODEType_EndNode		= 4;
	kSAXNODEType_Value		= 5;

VAR
	hXML    : LONGINT;
	result  : INTEGER;
	xmlFile : STRING;
	xmlData	: DYNARRAY [] OF CHAR;


  PROCEDURE XMLSAXNodeCallback(
  			     XMLHandle      :LONGINT;
  			     nodeType       :INTEGER;  {one of kSAXNODEType_ constants}
		             nodeName       :STRING;
		             nodeValue      :STRING;
		             nodeAttrs      :DYNARRAY [] OF CHAR);

  VAR
	I, foundAt : INTEGER;
	strType, attrName, attrValue : STRING;
	attrs, temp : DYNARRAY [] OF CHAR;
	foundAt1, foundAt2 : INTEGER;

  BEGIN
	CASE nodeType OF
		kSAXNODEType_StartDoc:	strType := 'StartDoc';
		kSAXNODEType_EndDoc:	strType := 'EndDoc';
		kSAXNODEType_StartNode:	strType := 'StartNode';
		kSAXNODEType_EndNode:	strType := 'EndNode';
		kSAXNODEType_Value:	strType := 'Value';
	END;

	AlrtDialog( Concat( 'type=', strType, Chr(13), 'node=', nodeName,  Chr(13), 'value=', nodeValue ) );

	IF Len(nodeAttrs) > 0 THEN BEGIN
		temp := nodeAttrs;
		attrs := '';
		{extract the attribute names and values for Ctr(13) delimited string}
		{<node name>Ctr(13)<nodeValue>Chr(13)}
		WHILE Len( temp ) > 0 DO BEGIN
			foundAt1 := Pos( Chr(13), temp );
			IF foundAt1 > 0 THEN BEGIN
				attrName := Copy( temp, 1, foundAt1 - 1 );
				{nodeAttrs := Copy( nodeAttrs, foundAt1 + 1, Len(nodeAttrs) - foundAt1 - 2 );}
				Delete( temp, 1, foundAt1 );
			END;

			foundAt2 := Pos( Chr(13), temp );
			IF foundAt2 > 0 THEN BEGIN
				attrValue := Copy( temp, 1, foundAt2 - 1 );
				{nodeAttrs := Copy( nodeAttrs, foundAt2 + 1, Len(nodeAttrs) - foundAt2 - 2 );}
				Delete( temp, 1, foundAt2 );
			END;

			IF (foundAt1 > 0) AND (foundAt2 > 0) THEN BEGIN
				attrs	:= Concat( attrs, ' ', attrName, '=', attrValue );
			END;
		END;

		AlrtDialog( Concat( 'node=', nodeName,  Chr(13), 'has attributes: ', attrs ) );
	END;
  END;


BEGIN
	hXML := InitXML;

	xmlFile := 'XML Test File.xml';
	result := XMLSAXParseFile( hXML, kExternalsFolder, xmlFile, XMLSAXNodeCallback );

	result := ReleaseXML(hXML);

END;
RUN(XMLSaxTest_Read);

Python


Version

Availability: from Vectorworks 2011

See Also

[{VS:InitXML]] | VS:ReleaseXML | VS:XMLSAXParseMemory

[{VS:InitXML]] | [[VS:ReleaseXML]] | [[VS:XMLSAXParseMemory]]