如果 xml 节点不在列表中,则追加该节点
append xml node if it not present in the list
我有代码块:
object XmlExample {
def main(args: Array[String]): Unit = {
val someXml =
<books>
<book title="The Woman in White">
<author>Wilkie Collins</author>
</book> <book title="Great Expectations">
<author>Charles Dickens</author>
</book>
</books>
println("The xml object is of type: " + someXml.child)
}
}
我想检查节点 <c1>
是否作为其子节点存在,然后我将其添加为 <c1>Null</c1>
我不确定是否完全理解不存在 因为它的child 。 .. 或你所说 的确切含义我添加了它 但这是我对 添加可选的直接 child 书籍的直接回答 :
def addC1IfNotHere(someXml: scala.xml.Elem) = (someXml \ "c1") match {
case Seq() =>
someXml.copy(child = <c1>Null</c1> +: someXml.child)
case _ => someXml
}
这就像:
val someXmlWithC1 =
<books>
<c1>anything else</c1>
<book title="The Woman in White">
<author>Wilkie Collins</author>
</book> <book title="Great Expectations">
<author>Charles Dickens</author>
</book>
</books>
val someXmlWithoutC1 =
<books>
<book title="The Woman in White">
<author>Wilkie Collins</author>
</book> <book title="Great Expectations">
<author>Charles Dickens</author>
</book>
</books>
val hasItsOriginalC1 = addC1IfNotHere(someXmlWithC1)
val hasANewC1 = addC1IfNotHere(someXmlWithoutC1)
println(hasItsOriginalC1)
println(hasANewC1)
应该正常打印:
<books>
<c1>anything else</c1>
<book title="The Woman in White">
<author>Wilkie Collins</author>
</book> <book title="Great Expectations">
<author>Charles Dickens</author>
</book>
</books>
<books><c1>Null</c1>
<book title="The Woman in White">
<author>Wilkie Collins</author>
</book> <book title="Great Expectations">
<author>Charles Dickens</author>
</book>
</books>
希望对您有所帮助。
当 c1 不在您预期的位置或其他地方时,请不要犹豫。
我有代码块:
object XmlExample {
def main(args: Array[String]): Unit = {
val someXml =
<books>
<book title="The Woman in White">
<author>Wilkie Collins</author>
</book> <book title="Great Expectations">
<author>Charles Dickens</author>
</book>
</books>
println("The xml object is of type: " + someXml.child)
}
}
我想检查节点 <c1>
是否作为其子节点存在,然后我将其添加为 <c1>Null</c1>
我不确定是否完全理解不存在 因为它的child 。 .. 或你所说 的确切含义我添加了它 但这是我对 添加可选的直接 child 书籍的直接回答 :
def addC1IfNotHere(someXml: scala.xml.Elem) = (someXml \ "c1") match {
case Seq() =>
someXml.copy(child = <c1>Null</c1> +: someXml.child)
case _ => someXml
}
这就像:
val someXmlWithC1 =
<books>
<c1>anything else</c1>
<book title="The Woman in White">
<author>Wilkie Collins</author>
</book> <book title="Great Expectations">
<author>Charles Dickens</author>
</book>
</books>
val someXmlWithoutC1 =
<books>
<book title="The Woman in White">
<author>Wilkie Collins</author>
</book> <book title="Great Expectations">
<author>Charles Dickens</author>
</book>
</books>
val hasItsOriginalC1 = addC1IfNotHere(someXmlWithC1)
val hasANewC1 = addC1IfNotHere(someXmlWithoutC1)
println(hasItsOriginalC1)
println(hasANewC1)
应该正常打印:
<books>
<c1>anything else</c1>
<book title="The Woman in White">
<author>Wilkie Collins</author>
</book> <book title="Great Expectations">
<author>Charles Dickens</author>
</book>
</books>
<books><c1>Null</c1>
<book title="The Woman in White">
<author>Wilkie Collins</author>
</book> <book title="Great Expectations">
<author>Charles Dickens</author>
</book>
</books>
希望对您有所帮助。 当 c1 不在您预期的位置或其他地方时,请不要犹豫。