如何使用php创建基于模式的xml文档?

时间:2022-10-30 13:58:43

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

我想从用户提供的数据创建一个xml文件。

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 .

我可以使用simplexml或DOMDocument来创建xml文件,甚至在DOMDocument中还有一个选项来使用模式验证xml文档。

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?

但是,我需要的不是使用xml类创建节点和添加值,而是从存储在其他地方的与模式相关的数据中创建xml文件吗?

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.

我认为在。net中有一个选项可以从数据集读取到xml。但是我在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.

如果用户为pan、name、fathername、address、dob提供数据,那么我需要通过匹配模式和数据自动创建xml文档。

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.

模式可能会不时变化,所以我不想编辑创建/更改节点和属性的所有代码。我需要指出新的模式,以便代码基于此创建xml。

3 个解决方案

#1


1  

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.

请看https://github.com/moyarada/XSD-to-PHP,它从XSD编译PHP绑定,然后您可以将PHP类序列化为XML。

#2


0  

Your question does not have a simple answer. It can be done with meta programming, but it is going to be long. In fact, the more complicated your XSD is, the longer your code will be. Yet, if you use a simple XSD with primitive xs:* types, it is easy to define a converter from a PHP type into an XML string.

你的问题没有一个简单的答案。它可以用元编程来完成,但这将是一个很长的过程。实际上,XSD越复杂,代码就越长。然而,如果您使用一个简单的XSD和原始的xs:*类型,很容易从PHP类型定义一个转换器到一个XML字符串。

By parsing your XSD you can dynamically create an array, which will look like this hard-coded one:

通过解析XSD,您可以动态地创建一个数组,这个数组看起来就像这个硬编码的数组:

$meta = array(
  'name' => 'xs:string',
  'dob' => 'xs:date'
  );

If you user input is something like:

如果用户输入如下:

$input = array(
  'name' => 'Name',
  'dob' => new DateTime('30 years ago')
  );

Then you can produce dynamically that:

然后你可以动态地产生:

$output = array(  
  'name' => convert( $input['name'] , $meta['name'] ),  
  'dob' => convert( $input['dob'] , $meta['dob'] )  
  );  

The key is the convert function. By using the 'instanceof' operator and the 'is_*' functions you can determine the data type of the first argument. So all you have to do is to return an XML escaped string for each of the possible combinations:

关键是转换函数。通过使用“instanceof”运算符和“is_*”函数,可以确定第一个参数的数据类型。因此,您所要做的就是为每个可能的组合返回一个XML转义字符串:

php string --> xml 'xs:string'  
php DateTime --> xml 'xs:date' 

...

Then it is straight forward to create your final XML.

然后直接创建最终的XML。

#3


-1  

I think this should help you if you want to create XML files using PHP's DOM functions

我认为,如果您想使用PHP的DOM函数创建XML文件,这应该会对您有所帮助。

#1


1  

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.

请看https://github.com/moyarada/XSD-to-PHP,它从XSD编译PHP绑定,然后您可以将PHP类序列化为XML。

#2


0  

Your question does not have a simple answer. It can be done with meta programming, but it is going to be long. In fact, the more complicated your XSD is, the longer your code will be. Yet, if you use a simple XSD with primitive xs:* types, it is easy to define a converter from a PHP type into an XML string.

你的问题没有一个简单的答案。它可以用元编程来完成,但这将是一个很长的过程。实际上,XSD越复杂,代码就越长。然而,如果您使用一个简单的XSD和原始的xs:*类型,很容易从PHP类型定义一个转换器到一个XML字符串。

By parsing your XSD you can dynamically create an array, which will look like this hard-coded one:

通过解析XSD,您可以动态地创建一个数组,这个数组看起来就像这个硬编码的数组:

$meta = array(
  'name' => 'xs:string',
  'dob' => 'xs:date'
  );

If you user input is something like:

如果用户输入如下:

$input = array(
  'name' => 'Name',
  'dob' => new DateTime('30 years ago')
  );

Then you can produce dynamically that:

然后你可以动态地产生:

$output = array(  
  'name' => convert( $input['name'] , $meta['name'] ),  
  'dob' => convert( $input['dob'] , $meta['dob'] )  
  );  

The key is the convert function. By using the 'instanceof' operator and the 'is_*' functions you can determine the data type of the first argument. So all you have to do is to return an XML escaped string for each of the possible combinations:

关键是转换函数。通过使用“instanceof”运算符和“is_*”函数,可以确定第一个参数的数据类型。因此,您所要做的就是为每个可能的组合返回一个XML转义字符串:

php string --> xml 'xs:string'  
php DateTime --> xml 'xs:date' 

...

Then it is straight forward to create your final XML.

然后直接创建最终的XML。

#3


-1  

I think this should help you if you want to create XML files using PHP's DOM functions

我认为,如果您想使用PHP的DOM函数创建XML文件,这应该会对您有所帮助。