DOM 导航:消除文本节点
我有一个读取和解析 XML 的 js 脚本。 它从 XMLHttpRequest 请求(与返回 XML 的 php 脚本联系)获取 XML。 该脚本应该在第一个父节点下接收 2 个或更多节点。 它需要的 2 个节点具有明确定义的名称,其他节点可以是任何名称。 php 的输出可能是:
<?xml version='1.0'?>
<things>
<carpet>
<id>1</id>
<name>1</name>
<desc>1.5</desc>
</carpet>
<carpet>
<id>2</id>
<name>2</name>
<height>unknown</height>
</carpet>
</things>
这里所有地毯都有 7 个节点。
但也可能是:
<?xml version='1.0'?>
<things>
<carpet>
<id>1</id>
<name>1</name>
<desc>1.5</desc>
</carpet>
<carpet><id>2</id><name>2</name><height>unknown</height></carpet>
</things>
这里第一个地毯有 7 个节点,第二个地毯有 3 个节点。 我希望我的 javascript 代码能够以快速、干净的方式以完全相同的方式处理这两者。 如果可能的话,我想删除每个标签之间的所有文本节点。因此,像上面这样的代码总是会被视为:
<?xml version='1.0'?>
<things><carpet><id>1</id><name>1</name><desc>1.5</desc></carpet><carpet><id>2</id><name>2</name><height>unknown</height></carpet></things>
这是否可以以快速有效的方式实现?如果可能并且效率更高,我不想使用任何 get 函数(getElementsByTagName()、getElementById,...)。
I have a js script that reads and parses XML.
It obtains the XML from an XMLHttpRequest request (which contacts with a php script which returns XML).
The script is supposed to receive 2 or more nodes under the first parentNode.
The 2 nodes it requires have the name well defined, the other ones can be any name.
The output from the php may be:
<?xml version='1.0'?>
<things>
<carpet>
<id>1</id>
<name>1</name>
<desc>1.5</desc>
</carpet>
<carpet>
<id>2</id>
<name>2</name>
<height>unknown</height>
</carpet>
</things>
Here all carpets have 7 nodes.
but it also may be:
<?xml version='1.0'?>
<things>
<carpet>
<id>1</id>
<name>1</name>
<desc>1.5</desc>
</carpet>
<carpet><id>2</id><name>2</name><height>unknown</height></carpet>
</things>
Here the first carpet has 7 nodes, the 2nd carpet has 3 nodes.
I want my javascript code to treat both exactly the same way in a quick and clean way.
If possible, I'd like to remove all the text nodes between each tag. So a code like the one above would always be treated as:
<?xml version='1.0'?>
<things><carpet><id>1</id><name>1</name><desc>1.5</desc></carpet><carpet><id>2</id><name>2</name><height>unknown</height></carpet></things>
Is that possible in a quick and efficient way? I'd like not to use any get function (getElementsByTagName(), getElementById, ...), if possible and if more efficient.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
遍历 DOM 并删除您认为空的节点(仅包含空格)非常简单。
这是未经测试(已测试并已修复,此处实时复制 ),但它看起来像这样(显然,用符号替换那些幻数):我对“空白”的定义是根据 JavaScript 解释器对
\s
的理解,只有空格的任何东西(空格)RegExp 类。请注意,某些实现存在\s
包容性不够的问题(ASCII 范围之外的几个 Unicode“空白”字符不匹配等),因此请务必使用示例数据进行测试。It's pretty straightforward to walk the DOM and remove the nodes you consider empty (containing only whitespace).
This is untested(tested and fixed, live copy here), but it would look something like this (replace those magic numbers with symbols, obviously):There my definition of "blank" is anything which only has whitespace according to the JavaScript interpreter's understanding of the
\s
(whitespace) RegExp class. Note that some implementations have issues with\s
not being inclusive enough (several Unicode "blank" characters outside the ASCII range not being matched, etc.), so be sure to test with your sample data.我只想尝试一个非常粗略的字符串替换:假设您将其存储在一个名为
xml
的变量中:这是我整理的完整测试:
I would just try a very crude string replace: assuming you store this in a variable called
xml
:here's the complete test I put together: