将字符串转换为 xml 节点
我有字符串,它包含从 PHP 文件返回的 xml 节点。 就像
<a>1</a><b>0</b><c>4</c>..............
现在我需要找出每个节点有什么值,即a,b,c...... 将此字符串加载到 xmlDocument 时,我收到类似“有多个根元素”的错误。
任何解决方案
i have the string, it contains xml nodes, returned from the PHP file.
It's like
<a>1</a><b>0</b><c>4</c>..............
Now i need to find out what value each node have i.e a, b, c......
while loading this string to xmlDocument i'm getting error like "There are multiple root elements".
any solution for this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
格式良好的 XML 的基本规则之一是它具有单个根节点。在您的示例中,您有多个根:
为了使其格式良好,您必须使这些元素成为单个根的子元素:
格式不正确的 XML 文档根本不是真正的 XML 文档,您会发现任何 XML 解析器都无法读取它!
One of the basic rules for well-formed XML that it has a single root node. With your example, you have multiple roots:
To make it well-formed you will have to make these elements a child of a single root:
An XML document that is not well-formed is not really an XML document at all and you will find that no XML parser will be able to read it!
将其包装在根元素中。例如:
从
12...
到1< b>2...
然后正常计算。
Wrap it in a root element. E.g:
From
<a>1</a><b>2</b>...
To
<root><a>1</a><b>2</b>...</root>
Then compute as normal.
这是因为每个元素都处于同一水平。需要有一个包含所有这些的“根”。将它们包装在任意节点中,例如
...
,然后将新字符串加载到 xmlDocument。That is because each element is at the same level. There need to be a "root" that encloses all of them. Wrap them in a arbitrary node, say
<root>...</root>
then load the new string to the xmlDocument.这看起来像 XML,但它不是有效的 XML。在 XML 中,您有一个包含所有其他元素的根元素。
因此,如果
str
中有该字符串,请执行以下操作:This seems like XML, but it's not valid XML. In XML, you have a root element that wraps all of the other elements.
So, if you have that string in
str
, do this: