删除 xml 元素后删除空白行?
我用 simpledom 从 xml 文件中删除了一些元素。
代码:
$this->xmlDocument->removeNodes("//entity[name='mac']");
这是初始文件:
<entity id="1000070">
<name>apple</name>
<type>category</type>
<entities>
<entity id="7002870">
<name>mac</name>
<type>category</type>
</entity>
<entity id="7024080">
<name>iphone</name>
<type>category</type>
</entity>
<entity id="7024080">
<name>ipad</name>
<type>category</type>
</entity>
</entities>
</entity>
之后的文件:
<entity id="1000070">
<name>apple</name>
<type>category</type>
<entities>
<entity id="7024080">
<name>iphone</name>
<type>category</type>
</entity>
<entity id="7024080">
<name>ipad</name>
<type>category</type>
</entity>
</entities>
</entity>
我想知道如何删除运行删除代码后留下的空白行?
谢谢!
i removed some elements from a xml file with simpledom.
the code:
$this->xmlDocument->removeNodes("//entity[name='mac']");
here is the initial file:
<entity id="1000070">
<name>apple</name>
<type>category</type>
<entities>
<entity id="7002870">
<name>mac</name>
<type>category</type>
</entity>
<entity id="7024080">
<name>iphone</name>
<type>category</type>
</entity>
<entity id="7024080">
<name>ipad</name>
<type>category</type>
</entity>
</entities>
</entity>
the file afterwards:
<entity id="1000070">
<name>apple</name>
<type>category</type>
<entities>
<entity id="7024080">
<name>iphone</name>
<type>category</type>
</entity>
<entity id="7024080">
<name>ipad</name>
<type>category</type>
</entity>
</entities>
</entity>
i wonder how i could also remove the blank lines that are left after i ran the removal code?
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 XPath 将空白文本节点作为目标。
如果要删除的节点是
//entity[name='mac']
那么它前面的空白文本节点就是 然后,您可以使用
|< 删除两个节点集/code> 运算符,您的命令将变为:
You can target the whitespace text nodes with XPath.
If the nodes you want to remove are
//entity[name='mac']
then the whitespace text node before it would beAnd then, you can remove both nodesets by using the
|
operator and your command becomes:如果您想删除输出中的所有这些空格,可以使用以下简单方法:
Just in case you'd like to remove ALL these blank spaces for your output, there's a simple way below: