是否有获取 XML 节点的平面字符串表示形式的默认机制?
我不想使用 jQuery 进行以下操作:
如果我有以下 XML
<parent>
<a >some text
<b propA="foo">some more text
</b>
and more text still
</a>
</parent>
并想要调用类似
//pseudo... 的内容,我会传入节点 1,因为节点 0 是无用的空文本节点...
getFlatXMLAsString(parent .childNodes[1])
并接收一个看起来像 "some text\nsome more text\n\nand more text 的字符串still"
请注意它如何将 XML 转换为平面字符串,但包括节点自己的开始和结束标记(如果有属性,那么它也会有这些属性)。
显然我可以手动完成此操作,但希望有一些内置机制可以防止我重新发明轮子。
I don't want to use jQuery for the following:
If I had the following XML
<parent>
<a >some text
<b propA="foo">some more text
</b>
and more text still
</a>
</parent>
and wanted to call something like
//pseudo... I pass in node 1 because node 0 is a useless empty text node...
getFlatXMLAsString(parent.childNodes[1])
and receive a string that looked like "<a>some text\n<b propA="foo">some more text\n\n</b>and more text still</a>"
Notice how it just converts the XML to a flat string, but includes the node's own opening and closing tags (and if that had attributes, it would have those in there as well).
Obviously I can do this manually but was hoping there's some built in mechanism that would prevent me re-inventing the wheel.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据此处发布的答案,跨浏览器的方法可以做到这一点看起来就是
这样,如果我正确理解了这个问题。
According to the answer posted here the cross-browser way to do this looks like
That is if I understand the question correctly.
在 Mozilla 和 Webkit 浏览器中,您可以使用
XMLSerializer
。只需进行对象测试即可。我不知道其他浏览器有哪些功能,但他们可能有。
最坏的情况是,您必须编写一个函数来手动执行此操作,该函数遍历所有节点并写入字符串。 这个答案应该可以帮助您入门。
In Mozilla and Webkit browsers, you can use the
XMLSerializer
. Just do an object-test.I'm not aware of what facilities are available in other browsers, but they might have one.
Worst case scenario, you'll have to write a function to do this by hand which walks all the nodes and writes to a string. This answer should get you started.