Announcement

Collapse
No announcement yet.

Parsing XML Data

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

    Parsing XML Data

    I'm working on a module where I want to periodically grab XML data from another website and save it to my store database. I'm struggling with my options for parsing the XML, though. I'm currently using MvCALL to save the raw XML to a variable which I thought I could convert to an array with xml_parse_var. That doesn't seem to be working the way I expected, though. Is there any documentation for the xml_parse_var function?

    I previously worked around this problem by using PHP to grab the file and convert it to JSON, which Miva handles very easily. Is there currently any way to similarly convert XML to JSON within Miva, or simply parse the XML as an array like with miva_json_decode?

    Thanks,
    Josh

    #2
    We have a function we use to flatten the XML response to make it better parse into a miva structure. Here is an example which would need to be modified for your specific XML

    Code:
    <MvFUNCTION NAME = "XML_ParseAndFlatten_Response" PARAMETERS = "response_xml var, response var" STANDARDOUTPUTLEVEL = "">
        <MvIF EXPR = "{ NOT xml_parse_var( l.response_xml, l.xml_parse_output ) }">
            <MvEVAL EXPR = "{ xml_parse_error( l.error_lineno, l.error_message ) }">
            <MvFUNCTIONRETURN VALUE = "{ [ g.Module_Library_Utilities ].Error( 'MER-SHP-UPX-00045', 'XML Parse Error: Line ' $ l.error_lineno $ ': ' $ l.error_message ) }">
        </MvIF>
    
        <MvASSIGN NAME = "l.member_count"            VALUE = "{ miva_struct_members( l.xml_parse_output, l.members ) }">
    
        <MvIF EXPR = "{ l.member_count EQ 1 }">
            <MvASSIGN NAME = "l.toplevel_member"    VALUE = "{ l.members[ 1 ] }">
        <MvELSEIF EXPR = "{ ( l.member_count EQ 2 ) AND ( tolower( l.members[ 1 ] ) EQ 'provision' ) }">
            <MvASSIGN NAME = "l.toplevel_member"    VALUE = "{ l.members[ 2 ] }">
        <MvELSE>
            <MvFUNCTIONRETURN VALUE = "{ [ g.Module_Library_Utilities ].Error( 'MER-SHP-UPX-00046', 'XML response does not contain the expected number of toplevel members: ' $ l.members ) }">
        </MvIF>
    
        <MvREFERENCEARRAY NAME = "l.toplevel_xml" VARIABLE = "l.xml_parse_output">
            <MvMEMBER NAME = "{ l.toplevel_member }">
        </MvREFERENCEARRAY>
    
        <MvREFERENCEARRAY NAME = "l.toplevel_response" VARIABLE = "l.response">
            <MvMEMBER NAME = "{ l.toplevel_member }">
        </MvREFERENCEARRAY>
    
        <MvEVAL EXPR = "{ XML_Flatten( l.toplevel_xml, l.toplevel_response ) }">
    
        <MvFUNCTIONRETURN VALUE = 1>
    </MvFUNCTION>
    
    <MvFUNCTION NAME = "XML_Flatten" PARAMETERS = "xml var, output var" STANDARDOUTPUTLEVEL = "">
        <MvASSIGN NAME = "l.tag"                VALUE = "{ tolower( l.xml:name ) }">
        <MvASSIGN NAME = "l.value"                VALUE = "{ trim( l.xml:value ) }">
    
        <MvIF EXPR = "{ NOT ISNULL l.value }">
            <MvASSIGN NAME = "l.output" VALUE = "{ l.value }">
        <MvELSEIF EXPR = "{ ( l.tag EQ 'ratedshipment' ) OR
                            ( l.tag EQ 'packageresults' ) OR
                            ( l.tag EQ 'graphicimage' ) }">
            <MvASSIGN NAME = "l.elements"        VALUE = "{ miva_array_elements( l.output ) }">
    
            <MvIF EXPR = "{ l.elements LE 0 }">    <MvASSIGN NAME = "l.index" VALUE = 1>
            <MvELSE>                            <MvASSIGN NAME = "l.index" VALUE = "{ l.elements + 1 }">
            </MvIF>
    
            <MvFOREACH ITERATOR = "l.child" ARRAY = "l.xml:children">
                <MvREFERENCEARRAY NAME = "l.output_child" VARIABLE = "l.output">
                    <MvDIMENSION INDEX = "{ l.index }">
                    <MvMEMBER NAME = "{ l.child:name }">
                </MvREFERENCEARRAY>
    
                <MvEVAL EXPR = "{ XML_Flatten( l.child, l.output_child ) }">
            </MvFOREACH>
        <MvELSE>
            <MvFOREACH ITERATOR = "l.child" ARRAY = "l.xml:children">
                <MvREFERENCEARRAY NAME = "l.output_child" VARIABLE = "l.output">
                    <MvMEMBER NAME = "{ l.child:name }">
                </MvREFERENCEARRAY>
    
                <MvEVAL EXPR = "{ XML_Flatten( l.child, l.output_child ) }">
            </MvFOREACH>
        </MvIF>
    </MvFUNCTION>
    Brennan Heyde
    VP Product
    Miva, Inc.
    [email protected]
    https://www.miva.com

    Comment


      #3
      That makes much more sense now. I'll gladly swipe this code and adapt for my needs.Thanks, Brennan!

      Comment

      Working...
      X