从 XML 中删除节点 [英] Removing nodes from XML

查看:35
本文介绍了从 XML 中删除节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从另一个匹配指定标准的过滤子节点生成一个 XML 文档.我该怎么做?

解决方案

您可以使用 scala.xml.transform 中的 RuleTransformer.

假设你有一个带有remove"值的动作属性

<前><代码>val removeIt = 新重写规则{覆盖 def 变换(n:节点):NodeSeq = n 匹配 {case e: Elem if (e \ "@action").text == "remove" => NodeSeq.Empty案例 n => n}}新的 RuleTransformer(removeIt).transform(yourXML)

I want to produce an XML document from another, filtering subnodes that match a specified criterion. How should I do that?

解决方案

You can use RuleTransformer from scala.xml.transform.

Suppose you have action attribute with "remove" value


val removeIt = new RewriteRule {
    override def transform(n: Node): NodeSeq = n match {
      case e: Elem if (e \ "@action").text == "remove" => NodeSeq.Empty
      case n => n
    }
  }

new RuleTransformer(removeIt).transform(yourXML)

这篇关于从 XML 中删除节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆