如何在PHP SoapClient请求中添加type属性

时间:2017-02-20 22:51:33

标签: php arrays xml soap xsd

我需要使用PHP中的SoapClient实例来使用SOAP服务。

它会抛出此验证错误:

  

+“ValidationError”:“cvc-type.2:类型定义不能   元素ns1的抽象:风险。“

如何将type属性添加到数组中,如下所示(第一行,类型定义):

<xs1:risk xsi:type="xs:Car">
    <xs:base7>
        <xs:base7code>00054030014</xs:base7code>
    </xs:base7>
    <xs:plate>8726CJR</xs:plate>
    <xs:matriculationDate>2003-06-15</xs:matriculationDate>
    <xs:purchaseDate>2003-06-15</xs:purchaseDate>
    <xs:postalCode>03550</xs:postalCode>
    <xs:town>SANT JOAN D'ALACANT</xs:town>
    <xs:trailer>false</xs:trailer>
    <xs:garageType>10</xs:garageType>
    <xs:yearKilometers>28</xs:yearKilometers>
    <xs:habitualDriver>
        <xs1:identificationType>2</xs1:identificationType>
        <xs1:identification>85858585E</xs1:identification>
        <xs1:birthDate>1978-11-09</xs1:birthDate>
        <xs1:gender>1</xs1:gender>
        <xs1:personPermissionDate>1997-03-02</xs1:personPermissionDate>
        <xs1:personExpeditionZone>1</xs1:personExpeditionZone>
        <xs1:personMaritalStatus>3</xs1:personMaritalStatus>
    </xs:habitualDriver>
    <xs:owner>
        <xs1:identificationType>2</xs1:identificationType>
        <xs1:identification>85858585E</xs1:identification>
    </xs:owner>
</xs1:risk>

1 个答案:

答案 0 :(得分:0)

此脚本将您的xml文件转换为数组

class DaneParser 
 {
    public $arrayAttributes = array();
    public $attrName;

    public function __construct($xml) 
    {
        $this->parser = xml_parser_create();
        xml_set_object($this->parser, $this);
        xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
        xml_set_element_handler($this->parser, "startElement", "endElement");
        xml_set_character_data_handler($this->parser, "characterData");
        xml_parse($this->parser, $xml);
    }

    function startElement($parser, $name, $attributes) 
    {
         $name = explode(':',$name);
         $this->attrName = $name[1];
    }


    function endElement($parser, $name) 
    {

    }

    function characterData($parser, $data) 
    {
        if(!isset($this->arrayAttributes[$this->attrName]))
            $this->arrayAttributes[$this->attrName] = $data;
    }

}//END class `enter code here`

//Open xml file
$xml = file_get_contents("xml1.xml");   

//run sax
$pp = new DaneParser($xml);

//Print array
echo '<pre>';
print_r($pp->arrayAttributes);
echo '</pre>';