2

I need to convert .xsd file into array using PHP. I have dynamic xsd from user side and i need php code that will generate the array in 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>
Parvez1487
  • 47
  • 1
  • 5

3 Answers3

1

This will do the trick.

<?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); 
?>
S.Visser
  • 4,645
  • 1
  • 22
  • 43
1

try this

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;
}
Rejeesh Prarath
  • 497
  • 5
  • 8
0

Hop this help you

<?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); 
?>
webGautam
  • 555
  • 1
  • 3
  • 14
  • i am getting error, can you change this code as per shared my xsd file with my question. – Parvez1487 May 23 '13 at 08:13
  • Modify this lines ------------ `$xsdstring = "yourfilename.xsd";` ` $attributeNodes = $xsdpath->query('//xs:element[@name="shiporder"]')->item(0); ` – webGautam May 23 '13 at 11:09
  • @S.Visser yeah man iam sorry i did not refresh my page so i could not see ur post after i post i see u hv already posted!! – webGautam May 23 '13 at 12:28