本文介绍了PHP如何将Soap XML与XML Schema(.xsd)链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个网址.

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

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

如何让这两个合作,以便可以通过PHP提取信息?

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

如何将所有信息从XML文件中提取到PHP对象或数组中.

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

推荐答案

我刚刚用以下代码回答了自己的问题:

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);

这将返回一个包含XML的数组,正是我想要使用的数组.

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

这篇关于PHP如何将Soap XML与XML Schema(.xsd)链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 07:39