PHP How to link Soap XML with XML Schema(.xsd)

前端 未结 1 1128
时光取名叫无心
时光取名叫无心 2020-12-20 10:31

I have two urls.

http://www.labs.skanetrafiken.se/v2.2/GetStartEndPoint.xsd

http://www.labs.skanetrafiken.se/v2.2/querypage.asp?inpPointFr=lund&inpPoint         


        
相关标签:
1条回答
  • 2020-12-20 11:02

    I just answered my own question with this code:

    /**
     * convert xml string to php array - useful to get a serializable value
     *
     * @param string $xmlstr
     * @return array
     * @author Adrien aka Gaarf
     */
    
    
    function xmlstr_to_array($xmlstr) {
        $doc = new DOMDocument();
        $doc->loadXML($xmlstr);
        return domnode_to_array($doc->documentElement);
    }
    function domnode_to_array($node) {
        $output = array();
        switch ($node->nodeType) {
            case XML_CDATA_SECTION_NODE:
            case XML_TEXT_NODE:
            $output = trim($node->textContent);
            break;
            case XML_ELEMENT_NODE:
            for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) {
                $child = $node->childNodes->item($i);
                $v = domnode_to_array($child);
                if(isset($child->tagName)) {
                    $t = $child->tagName;
                    if(!isset($output[$t])) {
                        $output[$t] = array();
                    }
                    $output[$t][] = $v;
                }
                elseif($v) {
                    $output = (string) $v;
                }
            }
            if(is_array($output)) {
                if($node->attributes->length) {
                    $a = array();
                    foreach($node->attributes as $attrName => $attrNode) {
                        $a[$attrName] = (string) $attrNode->value;
                    }
                    $output['@attributes'] = $a;
                }
                foreach ($output as $t => $v) {
                    if(is_array($v) && count($v)==1 && $t!='@attributes') {
                        $output[$t] = $v[0];
                    }
                }
            }
            break;
        }
        return $output;
    }
    
    
    
    
    $xml = 'http://www.labs.skanetrafiken.se/v2.2/querypage.asp?inpPointFr=lund&inpPointTo=ystad';      
    
    $xmlstr = new SimpleXMLElement($xml, null, true);
    
    $array = xmlstr_to_array($xmlstr->asXML());
    
    print_r($array);
    

    This returns an array with the XML, exactly what I want to be able to work with.

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