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>
3
  • Does not make any sense. Did you mean type instead of array? Does PHP even have types? Commented May 23, 2013 at 7:29
  • have you checked stackoverflow.com/questions/2263771/… Commented May 23, 2013 at 7:30
  • yes,but not useful for me. Commented May 23, 2013 at 8:17

3 Answers 3

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); 
?>
Sign up to request clarification or add additional context in comments.

2 Comments

i am getting error, i have shared my xsd file with my question.
Error is "Trying to get property of non-object".
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;
}

Comments

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

3 Comments

i am getting error, can you change this code as per shared my xsd file with my question.
Modify this lines ------------ $xsdstring = "yourfilename.xsd"; ` $attributeNodes = $xsdpath->query('//xs:element[@name="shiporder"]')->item(0); `
@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!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.