Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Added support for encoding and decoding namespaced xml (xmlns) #11375

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

Merged
merged 1 commit into from
Jul 25, 2014
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,22 @@ public function decode($data, $format, array $context = array())
// todo: throw an exception if the root node name is not correctly configured (bc)

if ($rootNode->hasChildNodes()) {
return $this->parseXml($rootNode);

$xpath = new \DOMXPath($dom);
$data = array();
foreach ($xpath->query('namespace::*', $dom->documentElement) as $nsNode) {
$data['@'.$nsNode->nodeName] = $nsNode->nodeValue;
}

if (isset($data['@xmlns:xml'])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isset is unnecessary... unset will work without warning if the key is defined or not

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed this in 21e7ad7

unset($data['@xmlns:xml']);
}

if (empty($data)) {
return $this->parseXml($rootNode);
}

return array_merge($data, (array) $this->parseXml($rootNode));
}

if (!$rootNode->hasAttributes()) {
Expand Down Expand Up @@ -227,7 +242,7 @@ final protected function isElementNameValid($name)
{
return $name &&
false === strpos($name, ' ') &&
preg_match('#^[\pL_][\pL0-9._-]*$#ui', $name);
preg_match('#^[\pL_][\pL0-9._:-]*$#ui', $name);
}

/**
Expand Down Expand Up @@ -281,11 +296,11 @@ private function parseXmlAttributes(\DOMNode $node)

$data = array();

foreach ($node->attributes as $attrkey => $attr) {
foreach ($node->attributes as $attr) {
if (ctype_digit($attr->nodeValue)) {
$data['@'.$attrkey] = (int) $attr->nodeValue;
$data['@'.$attr->nodeName] = (int) $attr->nodeValue;
} else {
$data['@'.$attrkey] = $attr->nodeValue;
$data['@'.$attr->nodeName] = $attr->nodeValue;
}
}

Expand Down
63 changes: 63 additions & 0 deletions src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ public function testEncode()
$this->assertEquals($source, $this->encoder->encode($obj, 'xml'));
}

public function testEncodeWithNamespace()
{
$source = $this->getNamespacedXmlSource();
$array = $this->getNamespacedArray();

$this->assertEquals($source, $this->encoder->encode($array, 'xml'));
}

public function testEncodeSerializerXmlRootNodeNameOption()
{
$options = array('xml_root_node_name' => 'test');
Expand Down Expand Up @@ -253,6 +261,14 @@ public function testDecodeCdataWrappingAndWhitespace()
$this->assertEquals($expected, $this->encoder->decode($xml, 'xml'));
}

public function testDecodeWithNamespace()
{
$source = $this->getNamespacedXmlSource();
$array = $this->getNamespacedArray();

$this->assertEquals($array, $this->encoder->decode($source, 'xml'));
}

public function testDecodeScalarWithAttribute()
{
$source = '<?xml version="1.0"?>'."\n".
Expand Down Expand Up @@ -414,6 +430,53 @@ protected function getXmlSource()
'</response>'."\n";
}

protected function getNamespacedXmlSource()
{
return '<?xml version="1.0"?>'."\n".
'<response xmlns="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns:media="http://search.yahoo.com/mrss/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:yt="http://gdata.youtube.com/schemas/2007">'.
'<qux>1</qux>'.
'<app:foo>foo</app:foo>'.
'<yt:bar>a</yt:bar><yt:bar>b</yt:bar>'.
'<media:baz><media:key>val</media:key><media:key2>val</media:key2><item key="A B">bar</item>'.
'<item><title>title1</title></item><item><title>title2</title></item>'.
'<Barry size="large"><FooBar gd:id="1"><Baz>Ed</Baz></FooBar></Barry></media:baz>'.
'</response>'."\n";
}

protected function getNamespacedArray()
{
return array(
'@xmlns' => 'http://www.w3.org/2005/Atom',
'@xmlns:app' => 'http://www.w3.org/2007/app',
'@xmlns:media' => 'http://search.yahoo.com/mrss/',
'@xmlns:gd' => 'http://schemas.google.com/g/2005',
'@xmlns:yt' => 'http://gdata.youtube.com/schemas/2007',
'qux' => "1",
'app:foo' => "foo",
'yt:bar' => array("a", "b"),
'media:baz' => array(
'media:key' => "val",
'media:key2' => "val",
'A B' => "bar",
'item' => array(
array(
'title' => 'title1',
),
array(
'title' => 'title2',
)
),
'Barry' => array(
'@size' => 'large',
'FooBar' => array(
'Baz' => 'Ed',
'@gd:id' => 1,
),
),
),
);
}

protected function getObject()
{
$obj = new Dummy();
Expand Down