ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

XML-INTO for mixed XML data

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • XML-INTO for mixed XML data

    Hi
    I have below XML document and trying to parse it in RPGLE but somehow program is failing.

    XML SOAP response -
    <Response uniqueId="abc"><Defaults perQuantity="1" orderVolume="1.0" date="20170928" getElements="false" getScales="false"></Defaults><EvaluationResult evaluationMethod="TWN Xaw 1678" getElements="true" value="48.453213144117356"><Dimension name="DO_CUSTOMER" value="0000033486"></Dimension><Dimension name="PRODUCT" value="63133490"></Dimension>

    <Element value="56.23028170978334" name="Final Expert Price"></Element>
    <Element value="0.97" name="Calc Fin Target"></Element>


    <EvaluationMethodElement name="Quantity' " value="000005 "></EvaluationMethodElement></EvaluationR'esult></Response> '

    I only need element value and element name so just wanted to know there a easy way to parse response ignoring dimension response etc other parameters.
    I tried many ways but getting errors. I am trying to parse variable received from http_post_xml API.

  • #2
    Looks like you need XML-SAX for this if all you want is the element names and data. I wrote this utility - http://partner400.com/Examples/XMLSAXTST.htm - it does basically what you want and just prints what it finds. There was an article associated with it but it appears to have vanished from the web.

    But this one should give you what you need to know to understand the code: https://www.itjungle.com/2016/06/14/fhg061416-story01/

    Comment


    • #3
      Hi Instrucrat3....

      From my own experiences with XML-INTO... I would believe that maybe you are missing the point of how XML-INTO works. Your import of an XML document should use a data structure that includes ALL the fields, contained in a single data structure. This DS could even be in an COPY or INCLUDE member, that could be used in several programs that use the "Response" XML.

      Without getting into sub-procedures and all the other stuff you could bind to your program... the advantage of having the whole DS of the "Response" XML available for all XML-INTO programs would allow you to have ALL the elements available for future changes within every program that uses the "Response" XML.

      There is not a need to go selectively picking individual fields for a specific purpose within XML-INTO for (in my opinion that is) for various and individual specific programs. All this "selective picking" does is create this huge cluster-mess for future development within several programs that use the "Response" XML document.

      OK... I've said all that three times...

      There are ways to set up an XML DS to look for specific fields in an XML document... the options of XML-INTO include things like "allow-missing", which means that the XML DS things that are not hard coded into a specific program are "allowed to be missing in this specific program". However, there again, if you choose this path... future program changes for these programs could create a real mess.

      I guess now I've made this point four times... O.o

      I would highly suggest that you look into Scott Klement's tutorials regarding XML. There are others, however, Scott's efforts have simple examples that are easily expanded into the area you are asking about.


      Best Regards and Good Luck!
      Fred Williams





      Comment


      • #4
        Agreed that I would normally use XML-INTO myself - but if indeed the task si as stated by the OP then XML-SAX fits the bill.

        One comment Fred on your statement "'allow-missing', which means that the XML DS things ... "allowed to be missing in this specific program". These days I do not recommend the use of allowmissing at all - the reason is that there is no granularity so the entire document could be missing (not just the bits you are OK with losing) and you would not know. At V5R4 you had no choice for optional elements etc. but these days they can be handled properly by using countprefix without the attendant risks. If only selective elements are needed then XML-SAX is a legitimate way to go.

        Comment


        • Whitecat27
          Whitecat27 commented
          Editing a comment
          Thanks Jon for the clarification. I hadn't thought about XML-INTO in the way you describe. Maybe it is just too many years of using V5R4.

          Best Regards and Good Luck!
          Fred Williams

        • JonBoy
          JonBoy commented
          Editing a comment
          Makes sense Fred. The V5R4 support was OK but the subsequent V6 and V7 enhancements have made it a really useful tool.

          Hopefully the OP will eventually come back and tell us what he meant. I hate it when people pose a question and then don't explain the answer. It means that somebody else will inevitably have to ask the same question as they won't find the answer here.

      • #5
        Thank you all.....saved variable to IFS and XML-INTO worked without any issue

        Comment


        • #6
          For the sake of future readers could you please explain a little more. There is nothing that can be processed with XML-INTO when reading from a file that cannot be done when reading from a variable - the _only_ difference is the source of the XML. So your comment saving it to the IFS allowed it to work makes no sense. All that would do is slow things down. Maybe you can post the code you used and then we can see why it didn't work originally.

          Comment


          • #7
            If the data structure doesn't have a subfield for everything that the XML has, then it needs "allowextra=yes". Me, I think it would be a lot easier to code a data structure with just the subfields that are needed than to use XML-SAX.

            Code:
            dcl-ds Response qualified;
               dcl-ds EvaluationResult;
                  dcl-ds EvaluationMethodElement;
                     name varchar(20);
                     value varchar(20);
                  end-ds;
               end-ds;
            end-ds;
            xml-into Response %xml('xmldoc.xml'
                                 : 'doc=file case=any allowextra=yes');
            dsply ('name  = '
                 + Response.EvaluationResult.EvaluationMethodElement.name);
            dsply ('value = '
                 + Response.EvaluationResult.EvaluationMethodElement.value);
            return;

            Comment


            • #8
              Good point Barbara - we got into "missing" whereas "extra" is the appropriate option if indeed the OP only wanted selective elements. But since the OP appears to have "walked away" we may never know!

              Comment

              Working...
              X