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

牧云@^-^@ 提交于 2019-12-29 08:25:13

问题


I have two urls.

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

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

How do I get these two to collaborate so I can extract the information via PHP?

How does one extract all the information out of the XML file into a PHP object or array.


回答1:


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.



来源:https://stackoverflow.com/questions/11861081/php-how-to-link-soap-xml-with-xml-schema-xsd

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!