2

我需要使用 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>
4

3 回答 3

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); 
?>
于 2013-05-23T07:39:36.630 回答
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;
}
于 2013-12-21T09:31:50.733 回答
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); 
?>
于 2013-05-23T07:42:53.690 回答