I need to get key/value pairs from XML to populate member info on a website. Here’s a sample of the XML:
<a:PObject xmlns:b="http://schemas.datacontract.org">
<b:CanUpsert>true</b:CanUpsert>
<b:Fields xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<c:KeyValueOfstringanyType>
<c:Key>FirstName</c:Key>
<c:Value i:type="d:long" xmlns:d="http://www.w3.org/2001/XMLSchema">Joe</c:Value>
</c:KeyValueOfstringanyType>
<c:KeyValueOfstringanyType>
<c:Key>LastName</c:Key>
<c:Value i:type="d:long" xmlns:d="http://www.w3.org/2001/XMLSchema">Mama</c:Value>
</c:KeyValueOfstringanyType>
</b:Fields>
</a:PObject>
I am new to XML and am having a tough time with the prefixes. I am using the following to give me an array of Key/Value pairs:
<cfset keyValue = xmlSearch(soapBody,"//*[local-name()='Key'] | //*[local-name()='Value']") />
I am able to reference the data I need by index; this works well for the most part, but for some reason not every record has its data in the same place. That is, #keyValue[4]# works in 95% of the records, but in a few, gives me a totally different value!
I have read many posts on this subject here and elsewhere; none of them get me around the prefix issue. For example, I can access an element by name using
<cfset firstNameKey = XmlSearch(soapBody,"//*[ text() = 'FirstName' ]") />
but how would I get the corresponding value? I’ve tried using following-sibling, but must not have done it correctly… I even tried stripping out all of the prefixes — nothing worked after that!
Thank you all so much in advance for any advice or suggestions you can give me. As I said, I am new to XML (and not that advanced in CF either) and am looking forward to getting pointed in the right direction. Thanks alot!
I would suggest a simpler means than xpath – convert the XML string to an object with xmlparse() and then iterate over the elements you need, using cf code to construct the name-value pairs. Cfdump the XML object to see what the resulting structure looks like – should be pretty straightforward. Give it a shot.
edit
After working on this a bit, I have some working code for you:
Note, I had to modify your xml sample slightly, since there were some references to namespaces that were not defined. Anyhow – the above works for me.