我正在使用scala.xml库。在我的例子中,我想检查每个“学生”是否定义参数“年龄”,但是值是什么并不重要。 在这种情况下,我的测试应该返回false:
<student name="James"
age="16"
class="4C"
/>
<student name="Ellen"
class="4C"
/>
但在这一个中是真的:
<student name="James"
age="16"
class="4C"
/>
<student name="Ellen"
age="15"
/>
我尝试过类似的东西,但它总是返回false:
var res : Boolean = true
for(node <- (xml \\ "student"))
{
if(!(node.contains("age"))) res = false
}
res
我本可以找到解决问题的方法,但语法效率不高。你觉得怎么样?
var res : Boolean = true
for(node <- (xml \\ "student"))
{
if(!(node.attribute("age").nonEmpty)) res = false
}
res
答案 0 :(得分:1)
更面向函数式编程的方法将在#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
int main(){
int input;
cout << "I want the number thats underneath this to not show up, but still be inputted." << endl;
cin >> input;
}//main
类型上使用forall
方法来查看给定条件对于序列中的所有项是否为真。
在您的情况下,您想知道是否为所有Seq
个节点定义了属性"age"
。
student
答案 1 :(得分:-1)
我找到了对我有用的东西。我不知道这是否是最好的方法,但它解决了我的问题所以也许它也会解决别人的问题。
var res : Boolean = true
for(node <- (xml \\ "student"))
{
if(!(node.attribute("age").isDefined)) res = false
}
res