How do I create variables from XML data in PHP?

后端 未结 9 1584
不思量自难忘°
不思量自难忘° 2021-01-25 18:34

Been trying to figure this out for a short while now but having now luck, for example I have an external xml document like this:


&         


        
相关标签:
9条回答
  • 2021-01-25 18:55

    Use SimpleXML to parse the file into an object/array structure, then simply use list:

    $sxml = new SimpleXMLElement($xml);
    $positions = (array)$sxml->positions->children();
    list($top_a, $top_b, $sidebar_a, $footer_a) = $positions['position'];
    
    0 讨论(0)
  • 2021-01-25 18:57

    Take a look at SimpleXML:

    http://www.php.net/manual/en/simplexml.examples-basic.php
    

    It parses XML into a "map-like" structure which you could then use to access your content. For your particular case,

    $xml = new SimpleXMLElement($xmlstr);
    
    $top_a =  $xml->template->positions[0] 
    
    0 讨论(0)
  • 2021-01-25 18:59

    You want the built in class Simplexml

    0 讨论(0)
  • 2021-01-25 19:01

    $str = "your xml";
    
    $xml = simplexml_load_string($str);
    $result = array();
    foreach ($xml->positions as $pos) {
        foreach ($pos->position as $p) {
            $element = (string)$p[0];
            $result[$element] = $element;
        }
    }
    
    var_dump($result);
    
    0 讨论(0)
  • 2021-01-25 19:02

    From the PHP web site at http://www.php.net/manual/en/function.xml-parse.php:

    Ashok dot 893 at gmail dot com 26-Apr-2010 05:52 This is very simple way to convert all applicable objects into associative array. This works with not only SimpleXML but any kind of object. The input can be either array or object. This function also takes an options parameter as array of indices to be excluded in the return array. And keep in mind, this returns only the array of non-static and accessible variables of the object since using the function get_object_vars().

    <?php
    function objectsIntoArray($arrObjData, $arrSkipIndices = array())
    {
        $arrData = array();
    
        // if input is object, convert into array
        if (is_object($arrObjData)) {
            $arrObjData = get_object_vars($arrObjData);
        }
    
        if (is_array($arrObjData)) {
            foreach ($arrObjData as $index => $value) {
                if (is_object($value) || is_array($value)) {
                    $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
                }
                if (in_array($index, $arrSkipIndices)) {
                    continue;
                }
                $arrData[$index] = $value;
            }
        }
        return $arrData;
    }
    ?>
    
    Usage:
    
    <?php
    $xmlUrl = "feed.xml"; // XML feed file/URL
    $xmlStr = file_get_contents($xmlUrl);
    $xmlObj = simplexml_load_string($xmlStr);
    $arrXml = objectsIntoArray($xmlObj);
    print_r($arrXml);
    ?>
    

    This will give the following result:

    Array
    (
        [name] => My Template Name
        [author] => John Doe
        [positions] => Array
            (
                [position] => Array
                    (
                        [0] => top-a
                        [1] => top-b
                        [2] => sidebar-a
                        [3] => footer-a
                    )
    
            )
    
    )
    
    0 讨论(0)
  • 2021-01-25 19:02
    $dom = new DOMDocument;
        $dom->loadXML('<root><position>a</position></root>'); //your string here 
        //$dom->loadXML(file_get_contents($file_with_pxml)); - from file
        $position = $dom->getElementsByTagName('position');
        for ($i=0; $i<$position->length; $i++)
        {
          $item = $position->item($i);
          ${$item->nodeValue} = $item->nodeValue;//$$item->nodeValue = $item->nodeValue;
        }
    

    But as I know - you can't create variable with dash in name in PHP

    0 讨论(0)
提交回复
热议问题