-
-
Notifications
You must be signed in to change notification settings - Fork 585
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
XML deserialization with inner XmlNamespaces does not work #994
Comments
attentiuon that see example in http://jmsyst.com/libs/serializer/master/reference/annotations#xmlnamespace |
Thanks for your response. But unfortunately the solution provided didn't work. The result is the same as before. Here is the code changed and the actual results: use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\Annotation as JMS;
require_once 'vendor/autoload.php';
/**
* @JMS\XmlNamespace(uri="http://www.example.org/abc")
* @JMS\XmlRoot("Simple")
*/
class Simple
{
/**
* @JMS\Type("string")
* @JMS\XmlElement(namespace="http://www.example.com/abc", cdata=false)
*/
public $property1;
/**
* @JMS\Type("string")
* @JMS\XmlElement(namespace="http://www.example.com/abc", cdata=false)
*/
public $property2;
}
/**
* @JMS\XmlRoot("Envelope")
*/
class Envelope
{
/**
* @JMS\Type("Simple")
* @JMS\SerializedName("Simple")
*/
public $simple;
}
$serializer = SerializerBuilder::create()->build();
$xmlWithEnvelope = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Simple xmlns="http://www.example.com/abc">
<property1>bar</property1>
<property2>foo</property2>
</Simple>
</Envelope>
XML;
$objectWithEnvelope = $serializer->deserialize($xmlWithEnvelope, Envelope::class, 'xml');
echo "initial XML:\n";var_dump($xmlWithEnvelope);
echo "\ndeserialized object:\n";var_dump($objectWithEnvelope);
echo "\nserialized XML:\n";var_dump($serializer->serialize($objectWithEnvelope, 'xml')); Result:
|
you forgot to add the NS on |
Thank you! I have added the NS on $simple prop and now it's deserializing well, but the serialization doesn't return the initial XML. Is there anything else that i'm missing? Here is the current output:
Thanks again for your support, appreciated. |
you can put (EDIT: probably you will have to put it on the |
Thanks for the suggestion but the initial XML is not generated from me. It's fetched from an external webservice. The idea is, given a PHP object like this
Obtain a serialized XML with this exact string value:
I mean, with a nested namespace without any prefix, and not getting the default namespace prefix. is this kind of serialization possible? Sorry for the inconvenience! Thank you so much :) |
Semantically <?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Simple xmlns="http://www.example.com/abc">
<property1>bar</property1>
<property2>foo</property2>
</Simple>
</Envelope> and <?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<p:Simple xmlns:p="http://www.example.com/abc">
<p:property1>bar</p:property1>
<p:property2>foo</p:property2>
</p:Simple>
</Envelope> are equivalent |
You're right, again :) Your last comment helped me to get a nice solution with my related issue. Thank you so much for your support and your work with this excellent library! |
I have a similar problem but mine is not solved. The only difference in my case is that xmlns attribute is defined in the root node "Envelope". @goetas I'm not sure to have well apply your suggestion #994 (comment). Could you please show the modification you've made ? @GonocraM deserialized object: |
Steps required to reproduce the problem
When we have an XML with a child node with a xml namespace the deserialization doesn't work as intended. I've seen this error only if the
xmlns
doesn't have prefix and it's in a child element, when it's in the root node works well in all of its forms.For Example
Expected Result
Actual Result
As we can see the object with the namespace is not deserialized
The text was updated successfully, but these errors were encountered: