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

在用simplexml_load_string时调用asXML会改变原有xml文本格式 #14846

Closed
DreamlingBig opened this issue Jul 6, 2024 · 2 comments
Closed

Comments

@DreamlingBig
Copy link

DreamlingBig commented Jul 6, 2024

Description

<?php

$xml = '<?xml version="1.0" encoding="UTF-8"?><root><teenMode>0</teenMode><M-Value>V/WvP1WCRxH9uDe6ZW6VrA==
</M-Value><uid>5470686000</uid><qq>861683052</qq>
<item cmd="1" optime="1714311948" QQ="861683052"/></root>';
$post_body = simplexml_load_string($xml);
$post_body->item['QQ'] = 99408162;
$post_body = $post_body->asXML();//Return a well-formed XML string based on SimpleXML element
print_r($post_body);

上面返回的结果(第一行后面应该是没有换行的,调用asXML后会改变了原有的格式)

<?xml version="1.0" encoding="UTF-8"?>
<root><teenMode>0</teenMode><M-Value>V/WvP1WCRxH9uDe6ZW6VrA==
</M-Value><uid>5470686000</uid><qq>861683052</qq>
<item cmd="1" optime="1714311948" QQ="99408162"/></root>

应该返回的结果

<?xml version="1.0" encoding="UTF-8"?><root><teenMode>0</teenMode><M-Value>V/WvP1WCRxH9uDe6ZW6VrA==
</M-Value><uid>5470686000</uid><qq>99408162</qq>
<item cmd="1" optime="1714311948" QQ="861683052"/></root>
@nielsdos
Copy link
Member

nielsdos commented Jul 6, 2024

There are two differences in what you expect vs what you got:

  • The <?xml version="1.0" encoding="UTF-8"?> is on a new line because whitespace after the xml delaration is not significant to an XML parser, and so they are not stored. That means that SimpleXML cannot know whether there was any whitespace, and it chooses to emit a newline for clarity. This has no impact on any parser and should thus not be a concern.
  • There's a newline after V/WvP1WCRxH9uDe6ZW6VrA== because in your original XML code you also have a newline after this string. Newlines inside elements are significant and thus are preserved.

So there is not a bug here. The first one doesn't matter and the second one can be fixed by removing the newline.

Copy link

No feedback was provided. The issue is being suspended because we assume that you are no longer experiencing the problem. If this is not the case and you are able to provide the information that was requested earlier, please do so. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants