Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i create a xml document based on a schema using php?

Tags:

php

xml

xsd

I want to create a xml file from the data given by the users.

I can use simplexml or DOMDocument for creating xml files and even there is an option in DOMDocument to verify the xml document with a schema .

But what i need is instead of creating nodes and adding values using xml classes, can i create a xml file from the data stored somewhere else in respect with a schema?

I think in .net there is an option to write into xml from reading from dataset.. But i couldn't find such thing in PHP.

Is that possible and are there any classes for that?

If there are no predefined classes , at least any help on any ways of doing that?

Edit:

I'm editing this question because it seems that some of you is not clear about my requirement..

For example, if the schema is

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="unqualified">
<xs:element name="formpatterns">
<xs:element name="pan" type="pantype"/>
<xs:element name="name" type="nametype"/>
<xs:element name="fatherName" type="nametype"/>
<xs:group ref="address"/>
<xs:element name="dob" type="xs:date"/>
</xs:schema>

and if user gives the data for pan, name, fathername, address, dob then i need to create the xml document automatically by matching the schema and data.

The schema may change from time to time , so i dont want to edit all the code to create/ change nodes and attributes. I need just to point the new schema , so that the code creates the xml based on that.

like image 269
Vijay Avatar asked Feb 09 '11 06:02

Vijay


People also ask

What is XML Schema in PHP?

An XML Schema describes the structure of an XML document, just like a DTD. An XML document with correct syntax is called "Well Formed". An XML document validated against an XML Schema is both "Well Formed" and "Valid".


1 Answers

Have a look at https://github.com/moyarada/XSD-to-PHP it compiles PHP bindings from XSD, and then you can serialize PHP classes to XML.

like image 129
Mike Bevz Avatar answered Sep 28 '22 14:09

Mike Bevz