[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Parsing XML from memory
I am using HTTPAPI (we currently have 1.21 installed) to call a web
service from RPG.
In the response that is received from this web service, there is a
chunk of XML that is wrapped in a CDATA block.
e.g (simplified!)
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:xyz="http://www.myservice.com" >
<soap:Header/>
<soap:Body>
<xyz:Message>
<xyz:MsgType>TYPE</xyz:MsgType>
<xyz:MsgData><![CDATA[
<messagexml><id>123456789</id></messagexml>]]></xyz:MsgData>
</xyz:Message>
</soap:Body>
</soap:Envelope>
My event handler happily picks up the end of the MsgData tag and
passes me the xml string as the value of the tag - however I now need
to parse this xml that is held in a variable in the RPG. I am trying
to use the bundled expat parser to do this but am encountering
difficulties in getting this to work.
The first iteration of the code for this that I have tried is as
follows (this is just a snippet of the critical portion!) :-
/FREE
MsgData = %trim(value);
parser = XML_ParserCreate(*OMIT);
XML_SetEndElementHandler(parser: %paddr(processMsgData));
rc = XML_Parse(parser : MsgData : %len(MsgData) : 0);
if (rc = XML_STATUS_ERROR);
errormsg = 'Parse error at line '
+ %char(XML_GetCurrentLineNumber(parser)) + ': '
+ %str(XML_ErrorString(XML_GetErrorCode(parser)));
endif;
XML_ParserFree(parser);
/END-FREE
If I run this, the parser returns an error "Parse error at line 1: not
well-formed (invalid token)".
I guessed that this may be down to the encoding, so I attempted to add
in code to use iconv to convert this to CCSID 1208.
This made the return code from the parser = 1. However the event
handler wasn't getting called as expected - rather than for the end of
each element, it was only called once - and it did not have any data
in the name or value parameters...
I then decided to try and extract this logic into a test program that
would just parse some XML held in an RPG variable (in case the nesting
of event handlers was causing a problem) - and I can't get the XML to
parse successfully. I've tried with and without converting the
encoding, but always get this parse error.
I'm therefore a little bit stumped. Does anyone have any working
examples of parsing XML held in a variable in RPG?
Thanks,
Stuart
-----------------------------------------------------------------------
This is the FTPAPI mailing list. To unsubscribe, please go to:
http://www.scottklement.com/mailman/listinfo/ftpapi
-----------------------------------------------------------------------