On 2019-07-12 5:40 PM, Justin Taylor wrote:
I can't seem to get the DS defined properly to parse this XML. I've never dealt with XML that had an attribute on parent item (the ID on Address), and I suspect that's my problem.
Here's the XML:
<AddressValidateResponse><Address ID="0"><Address2>6406 IVY LN</Address2><City>GREENBELT</City><State>MD</State><Zip5>20770</Zip5><Zip4>1441</Zip4></Address></AddressValidateResponse>
Here's the applicable RPG code:
Dcl-ds lAddrValidResponseDs qualified inz dim(1) ;
id int(10) ;
Dcl-ds address ;
...
Justin, "ID" is a child of Address, so you should define id as a
subfield of Address, not lAddrValidResponseDs.
Dcl-ds lAddrValidResponseDs qualified inz dim(1) ;
Dcl-ds address ;
id int(10) ;
...
But you have some other problems.
Your lAddressValidateResponseDs is an array, so XML-INTO is expecting an
outer element (with any name) to contain the data for the array.
If you array was called "arr", and you did xml-into arr, then XML-INTO
would expect the XML to look like this:
<something><arr>...</arr><arr>...</arr><arr>...</arr></something>
It's interpreting <AddrValidResponse> as the outer XML tag that would
contain the actual data for the data structure array.
The reason it's not giving an error is that it's not finding data for
any array element, so it is putting zero in the PSDS subfield that says
how many elements were set.
You could remove the DIM from the data structure, or specify an index
for the array on the XML-INTO operation if your XML doesn't represent an
array.
But wait, that's not all. You also need to code option 'case=any',
because your XML is in mixed case.
And finally, you're missing a subfield for the "state" in the XML.
Ops, not quite finally. You either need to give your data structure the
same name as the outer element in the XML, or you need to use the path
option so XML-INTO is happy that the XML matches your data structure.
This works:
Dcl-ds lAddrValidResponseDs qualified inz;
Dcl-ds address ;
id int(10) ; // move this
address2 varChar(255) ;
city varChar(255) ;
state varChar(255) ; // add this
zip5 packed(5) ;
zip4 packed(4) ;
End-ds ;
Xml-Into lAddrValidResponseDs %xml(lFileName
: 'doc=file case=any path=AddressValidateResponse');
As an Amazon Associate we earn from qualifying purchases.