How to change attribute on Scala XML Element

后端 未结 5 792
渐次进展
渐次进展 2020-12-13 04:41

I have an XML file that I would like to map some attributes of in with a script. For example:


  
         


        
5条回答
  •  旧巷少年郎
    2020-12-13 05:13

    So if I were in your position, I think what I'd really want to be writing is something like:

    case elem: Elem => elem.copy(attributes=
      for (attr <- elem.attributes) yield attr match {
        case attr@Attribute("attr1", _, _) =>
          attr.copy(value=attr.value.text.toInt * 2)
        case attr@Attribute("attr2", _, _) =>
          attr.copy(value=attr.value.text.toInt * -1)
        case other => other
      }
    )
    

    There are two reasons this won't work out of the box:

    1. Attribute doesn't have a useful copy method, and
    2. Mapping over a MetaData yields an Iterable[MetaData] instead of a MetaData so even something as simple as elem.copy(attributes=elem.attributes.map(x => x)) will fail.

    To fix the first problem, we'll use an implicit to add a better copy method to Attribute:

    implicit def addGoodCopyToAttribute(attr: Attribute) = new {
      def goodcopy(key: String = attr.key, value: Any = attr.value): Attribute =
        Attribute(attr.pre, key, Text(value.toString), attr.next)
    }
    

    It can't be named copy since a method with that name already exists, so we'll just call it goodcopy. (Also, if you're ever creating values that are Seq[Node] instead of things that should be converted to strings, you could be a little more careful with value, but for our current purposes it's not necessary.)

    To fix the second problem, we'll use an implicit to explain how to create a MetaData from an Iterable[MetaData]:

    implicit def iterableToMetaData(items: Iterable[MetaData]): MetaData = {
      items match {
        case Nil => Null
        case head :: tail => head.copy(next=iterableToMetaData(tail))
      }
    }
    

    Then you can write code pretty much like what I proposed at the beginning:

    scala> val elem = 
    elem: scala.xml.Elem = 
    
    scala> elem.copy(attributes=
         |   for (attr <- elem.attributes) yield attr match {
         |     case attr@Attribute("attr1", _, _) =>
         |       attr.goodcopy(value=attr.value.text.toInt * 2)
         |     case attr@Attribute("attr2", _, _) =>
         |       attr.goodcopy(value=attr.value.text.toInt * -1)
         |     case other => other
         |   }
         | )
    res1: scala.xml.Elem = 
    

提交回复
热议问题