
Jan Schütze - 2012-09-13 15:04:07
I and my company use this (my) little class called craur to work with XML, json and even csv: https://github.com/DracoBlue/Craur
Looks like this:
$node = Craur::createFromHtml('<html><head><title>Hans</title></head><body>Paul</body></html>');
assert($node->get('html.head.title') == 'Hans');
assert($node->get('html.body') == 'Paul');
or even collections:
$node = Craur::createFromXml('<book><author>Hans</author><author>Paul</author></book>');
$authors = $node->get('book.author[]');
assert(count($authors) == 2);
If you want you can retrieve also multiple values at once:
$node = Craur::createFromJson('{"book": {"name": "MyBook", "authors": ["Hans", "Paul"]}}');
$values = $node->getValues(
array(
'name' => 'book.name',
'book_price' => 'price',
'first_author' => 'book.authors'
),
array(
'book_price' => 20
)
);
assert($values['name'] == 'MyBook');
assert($values['book_price'] == '20');
assert($values['first_author'] == 'Hans');
Regards,
Jan