Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a example 07 #37

Merged
merged 3 commits into from
Apr 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ vendor
.foundation
Makefile
.DS_Store
.vscode
75 changes: 75 additions & 0 deletions examples/example-read-07.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Examples for how to use CFPropertyList with xmlStream
* Read from a PropertyList with array
*
* @package plist
* @subpackage plist.examples
*/
namespace CFPropertyList;
use \XMLReader;

// just in case...
error_reporting( E_ALL );
ini_set( 'display_errors', 'on' );

/**
* Require CFPropertyList
*/
require_once(__DIR__.'/../classes/CFPropertyList/CFPropertyList.php');


/*
* create a new CFPropertyList instance which loads the sample.plist on construct.
* We don't know that it is a binary plist, so we simply call ->parse()
*/

$reader=new XMLReader();
$reader->open(__DIR__.'/sample.xml.array.plist');

while($reader->read()){
if($reader->depth==3 && $reader->nodeType==XMLReader::TEXT && $reader->value=='large_array'){
// array found!
break;
}
}

while($reader->read()){
if($reader->depth==3 && $reader->nodeType==XMLReader::ELEMENT && $reader->name=='dict'){
$stream = fopen('php://memory','r+');
// wrap it to mimic to be a plist xml doc
$foo='<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL.'<plist version="1.0">'.PHP_EOL.$reader->readOuterXml().PHP_EOL.'</plist>';
fwrite($stream,$foo );
rewind($stream);
$plist=new CFPropertyList();
$plist->loadXMLStream($stream);
fclose($stream);
echo '<pre>';
var_dump( $plist->toArray() );
echo '</pre>';
// first dict found! exit!
break;
}
}


//read remaining <dict> node until the end
while($reader->next('dict')){
if($reader->depth==3 && $reader->nodeType==XMLReader::ELEMENT && $reader->name=='dict'){
$stream = fopen('php://memory','r+');
// wrap it to mimic to be a plist xml doc
$foo='<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL.'<plist version="1.0">'.PHP_EOL.$reader->readOuterXml().PHP_EOL.'</plist>';
fwrite($stream,$foo );
rewind($stream);
$plist=new CFPropertyList();
$plist->loadXMLStream($stream);
fclose($stream);
echo '<pre>';
var_dump( $plist->toArray() );
echo '</pre>';
}

}


?>
66 changes: 66 additions & 0 deletions examples/sample.xml.array.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>large_array</key>
<array>
<dict>
<key>Year Of Birth</key>
<integer>1965</integer>
<key>Date Of Graduation</key>
<date>2004-06-22T19:23:43Z</date>
<key>Pets Names</key>
<array/>
<key>Picture</key>
<data>PEKBpYGlmYFCPA==</data>
<key>City Of Birth</key>
<string>Springfield</string>
<key>Name</key>
<string>John Doe</string>
<key>Kids Names</key>
<array>
<string>John</string>
<string>Kyra</string>
</array>
</dict>
<dict>
<key>Year Of Birth</key>
<integer>1975</integer>
<key>Date Of Graduation</key>
<date>2004-06-22T19:23:43Z</date>
<key>Pets Names</key>
<array/>
<key>Picture</key>
<data>PEKBpYGlmYFCPA==</data>
<key>City Of Birth</key>
<string>Springfield</string>
<key>Name</key>
<string>John Doe</string>
<key>Kids Names</key>
<array>
<string>John1</string>
<string>Kyra1</string>
</array>
</dict>
<dict>
<key>Year Of Birth</key>
<integer>1985</integer>
<key>Date Of Graduation</key>
<date>2004-06-22T19:23:43Z</date>
<key>Pets Names</key>
<array/>
<key>Picture</key>
<data>PEKBpYGlmYFCPA==</data>
<key>City Of Birth</key>
<string>Springfield</string>
<key>Name</key>
<string>John Doe</string>
<key>Kids Names</key>
<array>
<string>John2</string>
<string>Kyra2</string>
</array>
</dict>
</array>
</dict>
</plist>