SAX handler. package = org.xml.sax

It is strange concept initially, but easy enough once you get hands on and do it.

Here is an example of a simple handler that parses a specific XML response to create a dictionary using the element names as keys and the element content as values:

Attachment: SatoriZipTaskSaxHandler.java
Description: Binary data





and here is an example of a method that uses that handler to parse a xml response from an input stream buffered Reader and return the resulting dictionary.

protected NSDictionary<String, String> parseResponse(Reader responseReader) {
                XMLReader reader = WKXMLUtilities.createXMLReader();
                SatoriZipTaskSaxHandler handler = new SatoriZipTaskSaxHandler();
                reader.setContentHandler(handler);

                // Convert responseReader into an InputSource
                InputSource inputSource = new InputSource(responseReader);

                try {
                        reader.parse(inputSource);
                } catch (IOException e) {
                        throw new NestableRuntimeException(e);
                } catch (SAXException e) {
                        throw new NestableRuntimeException(e);
                }

                NSDictionary<String, String> results = handler.results();
                return results;
        }


It parses responses sth like this that I receive from a remote service

<ZIPTASK>
<ResponseFields>
<FLD_ADDRESS_BLOCK></FLD_ADDRESS_BLOCK>
<FLD_ ADDRESSLINE1>416 Lake t</FLD_ADDRESSLINE1>
<FLD_ADDRESSLINE2></FLD_ADDRESSLINE2>
<FLD_CARRIER_ROUTE>C004</FLD_CARRIER_ROUTE>
<FLD_CITY>Antioch</FLD_CITY>
<FLD_COUNTY_CODE>17097</FLD_COUNTY_CODE>
<FLD_COUNTY_NAME>Lake</FLD_COUNTY_NAME>
<FLD_DP_BARCODE>:600021406164:</FLD_DP_BARCODE>
<FLD_DPC>164</FLD_DPC>
<FLD_DPV_FOOTNOTE></FLD_DPV_FOOTNOTE>
<FLD_DPV_INDICATOR></FLD_DPV_INDICATOR>
<FLD_ERRORCODE>0</FLD_ERRORCODE>
<FLD_BUSINESS></FLD_BUSINESS>
<FLD_ZIPCODE>60002-1406</FLD_ZIPCODE>
<FLD_LASTLINE>Antioch IL 60002-1406</FLD_LASTLINE>
<FLD_LOT_NUMBER>D0029</FLD_LOT_NUMBER>
<FLD_CASSDATE>14486016</FLD_CASSDATE>
<FLD_POST_DIRECTIONAL></FLD_POST_DIRECTIONAL>
<FLD_PRE_DIRECTIONAL></FLD_PRE_DIRECTIONAL>
<FLD_PRIMARY_NUMBER>416</FLD_PRIMARY_NUMBER>
<FLD_RECORD_TYPE>10</FLD_RECORD_TYPE>
<FLD_STATE>IL</FLD_STATE>
<FLD_STREET_NAME>Lake</FLD_STREET_NAME>
<FLD_SUFFIX>St</FLD_SUFFIX>
<FLD_UNIT_NUMBER></FLD_UNIT_NUMBER>
<FLD_UNIT_DESIGNATOR></FLD_UNIT_DESIGNATOR>
<FLD_URBANIZATION></FLD_URBANIZATION>
<FLD_5DIGIT_CODED>1</FLD_5DIGIT_CODED>
<FLD_LACS_CODED>0</FLD_LACS_CODED>
<FLD_EWS_CODED>0</FLD_EWS_CODED>
<FLD_DPV_CODED>0</FLD_DPV_CODED>
<FLD_IS_RESIDENCE>0</FLD_IS_RESIDENCE>
<FLD_CONGRESSIONAL_DISTRICT>8</FLD_CONGRESSIONAL_DISTRICT>
<FLD_PMB_NUMBER></FLD_PMB_NUMBER>
</ResponseFields>
</ZIPTASK>



This is straight copy/paste from a working project.

ANyway, hope that helps a little,

Regards, Kieran

On Feb 10, 2009, at 6:28 PM, Josh Paul wrote:

What libraries/methods do you use for parsing incoming XML (i.e. via a REST call to an external service) and extracting values?
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/kieran_lists%40mac.com

This email sent to [email protected]

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to