Re: Parsing XML
Re: Parsing XML
- Subject: Re: Parsing XML
- From: Josh Paul <email@hidden>
- Date: Tue, 10 Feb 2009 19:37:00 -0800
Thanks for the response.
I've only glanced at the code, but does it handle situations such as:
<ResponseFields count=25>
<ResponseField id=1>
...
</ResponseField>
...
<ResponseField id=25>
...
</ResponseField>
</ResponseFields>
If so, how is the 'id' handled?
On Feb 10, 2009, at 6:17 PM, Kieran Kelleher wrote:
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:
<SatoriZipTaskSaxHandler.java>
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@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden