jenwachter / data-encoder
此包已被弃用且不再维护。未建议替代包。
一个PHP库,使用DOM Document对象创建XML,使操作更简单。
0.3
2013-05-01 19:49 UTC
Requires
- php: >=5.3.2
- doctrine/inflector: v1.0
This package is auto-updated.
Last update: 2023-03-14 16:37:26 UTC
README
最终这个库将扩展以支持各种类型的数据编码。目前只支持XML和RSS。
XML 示例
从一个数组开始,该数组按照您希望XML的结构来组织数据。请注意,应该包含像下面“posts”数组这样的对象组,其中键是复数。
$data = array( "something" => "something", "posts" => array( array( "title" => "This is the title", "author" => array( "firstName" => "John", "lastName" => "Smith" ), "url" => "http://www.somewebsite.com/path/to/article" ), array( "title" => "This is the title of another article", "author" => array( "firstName" => "Jane", "lastName" => "Doe" ), "url" => "http://www.somewebsite.com/path/to/this/article" ) ) );
然后创建一个新的XML编码器对象,传递数据数组给它。之后,调用 render()
方法就非常简单了。
$xml = new \DataEncoder\XML($data); $xml->render();
RSS 示例
创建RSS源的过程略有不同,因为RSS源有非常具体的要求。当实例化一个新的RSS编码器时,有三个必需参数和一个可选参数。在这个例子中,我将展示方法调用,然后描述每个参数。
$xml = new \DataEncoder\RSS($channelElements, $itemElements, $data, $dataMap); $xml->render();
$channelElements
频道元素的关联数组。请注意,根据RSS规范,“title”、“link”和“description”元素是必需的。可选元素包括“lastBuildDate”、“language”等...
$channelElements = array( "title" => "The Title of my feed", "link" => "http://www.google.com", "description" => "The description of my feed", "lastBuildDate" => 1361242244 );
$itemElements
为了避免在实例化RSS编码器之前进行大量处理,只传递您想在源中包含的字段,第二个参数允许您选择要包含哪些元素。请注意,这些值对应于 $dataMap 数组,然后是对 $data 通过项键传入的项。
$itemElements = array("id", "title", "description", "link", "comments", "pubDate");
$data
源中项目数组的项。
$data = array( array( "title" => "This is the title", "author" => array( "firstName" => "John", "lastName" => "Smith" ), "url" => "http://www.somewebsite.com/path/to/article" ), array( "title" => "This is the title of another article", "author" => array( "firstName" => "Jane", "lastName" => "Doe" ), "url" => "http://www.somewebsite.com/path/to/this/article" ) );
$dataMap
为了进一步防止在实例化RSS编码器之前进行大量处理,最后一个(可选的)参数是一个关联数组,将 $data 数组中项的字段映射到RSS特定字段。
$dataMap = array( "headline" => "title", "body" => "description", "url" => "link", "publish_date" => "pubDate" );