Json Encode or Serialize an XML

后端 未结 5 1262
-上瘾入骨i
-上瘾入骨i 2020-12-22 06:01

I have some xml, this is a simple version of it.



  item one
  item two         


        
相关标签:
5条回答
  • 2020-12-22 06:18

    Here is a class I've found that is able to process XML into array very nicely: http://outlandish.com/blog/xml-to-json/ (backup). Converting to json is a matter of a json_encode() call.

    0 讨论(0)
  • 2020-12-22 06:21

    You can go the route with json_encode and json_decode and you can add the stuff you're missing because that json_encode-ing follows some specific rules with SimpleXMLElement.

    If you're interested into the rules and their details, I have written two blog-posts about it:

    • SimpleXML and JSON Encode in PHP – Part I
    • SimpleXML and JSON Encode in PHP – Part II

    For you perhaps more interesing is the third part which shows how you can modify the json serialization and provide your own format (e.g. to preserve the attributes):

    • SimpleXML and JSON Encode in PHP – Part III and End

    It ships with a full blown example, here is an excerpt in code:

    $xml = '<xml>
    <items>
      <item abc="123">item one</item>
      <item abc="456">item two</item>
    </items>
    </xml>';
    
    $obj = simplexml_load_string($xml, 'JsonXMLElement');
    
    echo $json = json_encode($obj, JSON_PRETTY_PRINT), "\n";
    
    print_r(json_decode($json, TRUE));
    

    Output of JSON and the array is as following, note that the attributes are part of it:

    {
        "items": {
            "item": [
                {
                    "@attributes": {
                        "abc": "123"
                    },
                    "@text": "item one"
                },
                {
                    "@attributes": {
                        "abc": "456"
                    },
                    "@text": "item two"
                }
            ]
        }
    }
    Array
    (
        [items] => Array
            (
                [item] => Array
                    (
                        [0] => Array
                            (
                                [@attributes] => Array
                                    (
                                        [abc] => 123
                                    )
    
                                [@text] => item one
                            )
    
                        [1] => Array
                            (
                                [@attributes] => Array
                                    (
                                        [abc] => 456
                                    )
    
                                [@text] => item two
                            )
    
                    )
    
            )
    
    )
    
    0 讨论(0)
  • 2020-12-22 06:21

    You need to call attributes() function.

    Sample code:

    $xmlString = '<xml>
    <items>
      <item abc="123">item one</item>
      <item abc="456">item two</item>
    </items>
    </xml>';
    
    $xml = new SimpleXMLElement($xmlString);
    
    foreach( $xml->items->item as $value){
    $my_array[] =  strval($value->attributes());
    }
    
    print_r($my_array);
    

    Eval

    0 讨论(0)
  • 2020-12-22 06:27
    $xml = new SimpleXMLElement($xmlString);
    

    $xml is now an object. To get the value of an attribute:

    $xml->something['id'];
    

    Where 'id' is the name of the attribute.

    0 讨论(0)
  • 2020-12-22 06:30

    While it's theoretically possible to write a generic conversion from XML to PHP or JSON structures, it is very hard to capture all the subtleties that might be present - the distinction between child elements and attributes, text content alongside attributes (as you have here) or even alongside child elements, multiple child nodes with the same name, whether order of child elements and text nodes is important (e.g. in XHTML or DocBook), etc, etc.

    If you have a specific format you need to produce, it will generally be much easier to use an API - like SimpleXML - to loop over the XML and produce the structure you need.

    You don't specify the structure you want to achieve, but the general approach given your input would be to loop over each item, and either access known attributes, or loop over each attribute:

    $sxml = simplexml_load_string( $xml );
    $final_array = array();
    foreach ( $sxml->items->item as $xml_item )
    {
        $formatted_item = array();
    
        // Text content of item
        $formatted_item['content'] = (string)$xml_item;
    
        // Specifically get 'abc' attribute
        $formatted_item['abc'] = (string)$xml_item['abc'];
        // Maybe one of the attributes is an integer
        $formatted_item['foo_id'] = (int)$xml_item['foo_id'];
    
        // Or maybe you want to loop over lots of possible attributes
        foreach ( $xml_item->attributes() as $attr_name => $attr_value )
        {
             $formatted_item['attrib:' . $attr_name] = (string)$attr_value;
        }
    
        // Add it to a final list
        $final_array[] = $formatted_item;
        // Or maybe you want that array to be keyed on one of the attributes
        $final_array[ (string)$xml_item['key'] ] = $formatted_item;
    }
    
    0 讨论(0)
提交回复
热议问题