• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Parsing XML

Guest
Feb 21, 2017 Feb 21, 2017

Copy link to clipboard

Copied

Hello all,

I am having a terrible time parsing an XML doc that is being returned from a service. I think it might be because I upgraded to CF11 and perhaps something is out of date.

Normally I try to get this figured out myself but I just have spent too much time on something I'm sure is very easy.

Here is what is being returned:

<?xml version="1.0" encoding="utf-8"?>

<WebServiceGeocodeResult version="4.10">

<QueryMetadata>

<TransactionId>512a8c85-8258-498c-88a2-c75289e3cb25</TransactionId>

<Version>4.1</Version>

<QueryStatusCodeValue>200</QueryStatusCodeValue>

<FeatureMatchingResultType>Success</FeatureMatchingResultType>

<FeatureMatchingResultCount>1</FeatureMatchingResultCount>

<TimeTaken>0.0390039</TimeTaken>

<ExceptionOccured>False</ExceptionOccured>

<Exception />

<ErrorMessage />

</QueryMetadata>

<InputAddress>

<StreetAddress>1 S MAIN ST Akron OH 44308</StreetAddress>

<City>Akron</City>

<State>OH</State>

<Zip>44308</Zip>

</InputAddress>

<OutputGeocodes>

<OutputGeocode>

<Latitude>41.0847797927252</Latitude>

<Longitude>-81.5166029848824</Longitude>

<NAACCRGISCoordinateQualityCode>02</NAACCRGISCoordinateQualityCode><NAACCRGISCoordinateQualityType>Parcel</NAACCRGISCoordinateQualityType>

<MatchScore>100</MatchScore>

<MatchType>Exact</MatchType>

<FeatureMatchingResultType>Success</FeatureMatchingResultType>

<FeatureMatchingResultCount>1</FeatureMatchingResultCount>

<FeatureMatchingGeographyType>Parcel</FeatureMatchingGeographyType>

<RegionSize>553.366764519364</RegionSize>

<RegionSizeUnits>Meters</RegionSizeUnits>

<MatchedLocationType>LOCATION_TYPE_STREET_ADDRESS</MatchedLocationType>

<ExceptionOccured>False</ExceptionOccured>

<Exception />

<ErrorMessage />

</OutputGeocode>

</OutputGeocodes>

</WebServiceGeocodeResult>

And here is the code that is parsing:

Application.Functions.Dump(HTTPObject, "HTTPObject");

  //Remove everything before "<?xml"

  HTTPContent = Trim(ReReplace(HTTPObject.FileContent, "^.*(<\?xml)", "\1", "All"));

  /* Error responses*/

  // Bad request. The parameters passed to the service did not match as expected. The Message should tell you what was missing or incorrect.

  if(HTTPObject.QueryMetadata.QueryStatusCodeValue eq 400)

  {

  if(IsDefined("xmlDoc.Error.Message"))

  {

  throwGeocodeErrorMessage(xmlDoc.Error.Message.XmlText);

  }

  else

  {

  throwGeocodeFailed("Could not find required XML fields. XML: '" & HTTPContent & "'");

  }

  }

  xmlDoc = XmlParse(HTTPContent);

  if (HTTPObject.QueryMetadata.QueryStatusCodeValue neq 200)

  {

  throwGeocodeFailed("Error status code returned. Status Code: '" & HTTPObject.Responseheader.Status_Code &

  "'. XML: '" & HTTPContent & "'");

  }

  // Make sure all the required fields are available

  else if(Not (

  IsDefined("xmlDoc.WebServiceGeocodeResult.QueryStatusCodeValue") and

  IsDefined("xmlDoc.WebServiceGeocodeResult.Latitude") and

  IsDefined("xmlDoc.WebServiceGeocodeResult.Longitude") and

  IsDefined("xmlDoc.WebServiceGeocodeResult.MatchedLocationType")

  )

  )

  {

  throwGeocodeFailed("Could not find required XML fields. XML: '" & HTTPContent & "'");

  }

  //Make sure the response is 200(Success)

  if(xmlDoc.WebServiceGeocodeResult.QueryStatusCodeValue.XmlText neq "200")

  {

  throwGeocodeFailed("QueryStatusCodeValue: '" & xmlDoc.WebServiceGeocodeResult.QueryStatusCodeValue.XmlText  &

  "' QueryStatusCodeName: '" & xmlDoc.WebServiceGeocodeResult.QueryStatusCodeName.XmlText  &

  "'. XML: '" & HTTPContent & "'");

  }

I think the issue is here: HTTPContent = Trim(ReReplace(HTTPObject.FileContent, "^.*(<\?xml)", "\1", "All"));

Because I am getting errors that suggests that nothing is being parsed properly:

Element QUERYMETADATA.QUERYSTATUSCODEVALUE is undefined in HTTPOBJECT.

Any help would be greatly appreciated!!!

Thanks.

Views

555

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Feb 23, 2017 Feb 23, 2017

If you use ColdFusion Help | XmlParse  on the response you can then get an object you can refer to.

Something like:

HTTPContent = xmlParse(HTTPObject.FileContent);

writeDump(HTTPContent);

You seem to have most of the bits in place, just in the wrong order. You parse the XML later, but it needs to be done earlier if you want to check the status code values.

Votes

Translate

Translate
Advocate ,
Feb 22, 2017 Feb 22, 2017

Copy link to clipboard

Copied

Is HTTPObject actually an CF object or just the raw XML?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 23, 2017 Feb 23, 2017

Copy link to clipboard

Copied

Hey haxtbh, it is the raw XML. Thanks.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 22, 2017 Feb 22, 2017

Copy link to clipboard

Copied

I would first dump out the xml and make sure you are receiving it properly.  Second, I would run xmlparse on that xml to convert it into a coldfusion xml object and dump that to make sure it is being parsed properly. 

I have never had to remove the xml declaration line so I am not sure you are doing that.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 23, 2017 Feb 23, 2017

Copy link to clipboard

Copied

Hey Dave,

Thanks for replying. Would you have a suggestion on how to code the xmlparse tag? I thought that was decommissioned and therefore have not used it in a while.

Thanks.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 23, 2017 Feb 23, 2017

Copy link to clipboard

Copied

If you use ColdFusion Help | XmlParse  on the response you can then get an object you can refer to.

Something like:

HTTPContent = xmlParse(HTTPObject.FileContent);

writeDump(HTTPContent);

You seem to have most of the bits in place, just in the wrong order. You parse the XML later, but it needs to be done earlier if you want to check the status code values.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 23, 2017 Feb 23, 2017

Copy link to clipboard

Copied

LATEST

haxtbh,

Thank you very much. I was able to play around with xmlparse and the order and get it to work.

Thank you so much!!!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation