如何在PHP中将.xsd文件转换为数组

时间:2013-05-23 07:27:05

标签: php xsd

我需要使用PHP将.xsd文件转换为数组。 我有来自用户端的动态xsd,我需要php代码,它将在php中生成数组。

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="shiporder">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="orderperson" type="xs:string"/>
      <xs:element name="shipto">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="address" type="xs:string"/>
            <xs:element name="city" type="xs:string"/>
            <xs:element name="country" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="item" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="title" type="xs:string"/>
            <xs:element name="note" type="xs:string" minOccurs="0"/>
            <xs:element name="quantity" type="xs:positiveInteger"/>
            <xs:element name="price" type="xs:decimal"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="orderid" type="xs:string" use="required"/>
  </xs:complexType>
</xs:element>

</xs:schema>

3 个答案:

答案 0 :(得分:1)

这样就可以了。

<?php 
$attributes = array(); 
$xsdstring = "yourfile.xsd"; 
$XSDDOC = new DOMDocument(); 
$XSDDOC->preserveWhiteSpace = false; 
if ($XSDDOC->load($xsdstring)) 
{ 
    $xsdpath = new DOMXPath($XSDDOC); 
    $attributeNodes = 
              $xsdpath-> 
              query('//xs:simpleType[@name="attributeType"]') 
              ->item(0); 
    foreach ($attributeNodes->childNodes as $attr) 
    { 
        $attributes[ $attr->getAttribute('value') ] = $attr->getAttribute('name'); 
    } 
    unset($xsdpath); 
} 
print_r($attributes); 
?>

答案 1 :(得分:1)

试试这个

function xml2multiarray($xml){
    $xml_parser = xml_parser_create();
    xml_parse_into_struct($xml_parser, $xml, $xmlarray);
    $opened = array();
    $array = array();
    $arrsize = sizeof($xmlarray);
    for($j=0;$j<$arrsize;$j++){
        $val = $xmlarray[$j];
        switch($val["type"]){
            case "open":
                $opened[$val["tag"]] = $array;
                unset($array);
                break;
            case "complete":
                $array[$val["tag"]][] = $val["value"];
            break;
            case "close":
                $closed = $opened[$val["tag"]];
                $closed[$val["tag"]] = $array;
                $array = $closed;
            break;
        }
    }
    return $array;
}

答案 2 :(得分:0)

跳这个帮助你

<?php 
    $attributes = array(); 
    $xsdstring = "/htdocs/api/xsd/common.xsd"; 
    $XSDDOC = new DOMDocument(); 
    $XSDDOC->preserveWhiteSpace = false; 
    if ($XSDDOC->load($xsdstring)) 
    { 
        $xsdpath = new DOMXPath($XSDDOC); 
        $attributeNodes = 
          $xsdpath-> 
          query('//xs:simpleType[@name="attributeType"]') 
          ->item(0); 
        foreach ($attributeNodes->childNodes as $attr) 
        { 
            $attributes[ $attr->getAttribute('value') ] = $attr->getAttribute('name'); 
        } 
        unset($xsdpath); 
    } 
    print_r($attributes); 
?>