xmlGetLastError and TxmlError

BlitzMax Forums/BlitzMax Programming/xmlGetLastError and TxmlError

Scaremonger(Posted 2007) [#1]
When reading in an XML document with obvious errors in it, parseFile fails. So I try to get the last error, and it bombs out...

What am I doing wrong?

testXMLerror.bmx
Import BaH.Libxml

Local xmldoc:TxmlDoc = TxmlDoc.parseFile("testXMLerror.xml")
DebugLog "Loaded"
If xmldoc Then 
	DebugLog "XML Valid"
	Local root:TxmlNode = xmldoc.getRootElement()
	DebugLog root.getName()

	Local children:TList = root.getChildren()
	For Local node:TxmlNode = EachIn children
		DebugLog " =>" + node.getname()					
	Next
Else
	DebugLog "Invalid XML Syntax"
	Local err:TxmlError = xmlGetLastError()
	If err Then 
		DebugLog "ERROR MESSAGE"
		DebugLog err.getErrorMessage() 
		DebugLog "CODE"
		DebugLog String(err.getErrorCode()) 
	End If
End If
DebugLog "DONE"

testXMLerror.xml
<?xml version="1.0"?>
<!--This is a comment--> 
<Game>
	<test>
		<Element name=#testing#>
	</test>
</Game>



Brucey(Posted 2007) [#2]
What am I doing wrong?

You've found a bug :-p

A quick fix. Look for this in libxml.bmx :
Function xmlGetLastError:TxmlError()
	Return TxmlError._create(xmlGetLastError())
End Function

and change to this : (adding an underscore to the function call - otherwise it's just calling itself!!)
Function xmlGetLastError:TxmlError()
	Return TxmlError._create(_xmlGetLastError())
End Function


The expected output...
DebugLog:Loaded
DebugLog:Invalid XML Syntax
DebugLog:ERROR MESSAGE
DebugLog:Couldn't find end of Start Tag Element line 5

DebugLog:CODE
DebugLog:73
DebugLog:DONE


Sorry for the bug.


Scaremonger(Posted 2007) [#3]
Thanks very much...


Scaremonger(Posted 2007) [#4]
Still something a little funny going on here...

I notice in the output window that there is more error information provided after the program finished (After it's displayed the word "DONE").

Is there any way to get hold of that information instead of just "Couldn't find end of Start Tag Element line 5"?

Incidentally: If there are multiple errors, they are only shown at the end of program execution.

DebugLog:Loaded
DebugLog:No: Invalid
DebugLog:ERROR MESSAGE
DebugLog:Couldn't find end of Start Tag Element line 5

DebugLog:CODE
DebugLog:73
DebugLog:DONE
testXMLerror.xml:5: parser error : AttValue: " or ' expected
		<Element name=#testing#>
		              ^
testXMLerror.xml:5: parser error : attributes construct error
		<Element name=#testing#>
		              ^
testXMLerror.xml:5: parser error : Couldn't find end of Start Tag Element line 5
		<Element name=#testing#>
		              ^

Process complete

Also, try this faulty XML file:
<!- TEST ->
<?xml version="1.0"?>
<!--This is a comment--> 
<Game>
	<test>
		<Element name=#testing#>
	</test>
</Game>

It produces rather weird output that says there is an error at the end of the document, when the problem is in the first line...! (Not very helpful when tracking down an error in an XML file)
DebugLog:Loaded
DebugLog:No: Invalid
DebugLog:ERROR MESSAGE
DebugLog:Extra content at the end of the document

DebugLog:CODE
DebugLog:5
DebugLog:DONE
testXMLerror.xml:1: parser error : StartTag: invalid element name
<!- TEST ->
 ^
testXMLerror.xml:1: parser error : Extra content at the end of the document
<!- TEST ->
 ^



Scaremonger(Posted 2007) [#5]
Has anyone any experience with how other XML Libraries deal with these type of errors?


Brucey(Posted 2007) [#6]
Has anyone any experience with how other XML Libraries deal with these type of errors?


I suppose a different question here would go along the lines of, "Why doesn't the libxml module provide API access to the error callback stuff that comes with libxml?"

And I would probably reply with, "Given the size of libxml's API, it's quite possible that I was likely to miss things here or there, perhaps one of them being better access to the error reporting."

In order to right that wrong, I've updated (1.12) to add a new function xmlSetStructuredErrorFunc() as well as more "get" methods to TxmlError... and an example.

Sorry for your troubles.


Scaremonger(Posted 2007) [#7]
Thanks very much. Looks great...

Sorry if it's caused you hastle..