我有一个xml数据,我希望能够从中获取标记名并存储它们或根据数据中是否存在特定标记来运行操作
例如XML数据:
<MyData version="3.0">
<StudentData type="Table">
<Student type="Table">
<StudentValue type="Table">
<DateOfBirth type="Data">2009-05-31</DateOfBirth>
<StudenClass type="Data">Sixth</StudenClass>
<StudentRoll type="Data">006</StudentRoll>
<Name type="Table">
<FirstName type="Data">MMMMM</FirstName>
<LastName type="Data">XXXXXXX</LastName>
</Name>
<Performance type="Table">
<Level type="Data">Honor</Level>
</Performance>
</StudentValue>
<Contact type="Table">
<Email type="Data">xyx@xyz.com</Email>
</Contact>
</Student>
</StudentData>
但是我能够获取值和attr,如下面我一直使用的以下代码所示:
xml= '<MyData version="3.0"><StudentData type="Table"><Student type="Table"><StudentValue type="Table"><DateOfBirth type="Data">2009-05-31</DateOfBirth><StudenClass type="Data">Sixth</StudenClass><StudentRoll type="Data">006</StudentRoll><Name type="Table"><FirstName type="Data">MMMMM</FirstName><LastName type="Data">XXXXXXX</LastName></Name><Performance type="Table"><Level type="Data">Honor</Level></Performance></StudentValue><Contact type="Table"><Email type="Data">xyx@xyz.com</Email></Contact></Student></StudentData></MyData>';
var xmlData = cheerio.load(xml, {
ignoreWhitespace: true,
xmlMode: true
});
var studentName = xmlData('MyData StudentData Student StudentValue Name').eq(0).text();
var attr = xmlData('MyData StudentData Student StudentValue Name').eq(0).attr();
var find = xmlData('MyData StudentData Student StudentValue Name').get(0).tagname;
console.log(studentName);
console.log(attr);
console.log(find);
因此,我该如何获取和存储诸如StudentValue标记下的标记名“ Performance”之类的值。 我也想基于标记名/元素的存在来做一些动作。
就像JSON一样,我们可以按以下方式进行编码,以防出现问题:
if (MyData.StudentData.Student.StudentValue.hasOwnProperty('Performance'){
//then do some actions
console.log("Student is an Honor Student");
}
答案 0 :(得分:0)
您可以像这样在cheerio中获得标签名称
$('a').first().prop('tagName')
答案 1 :(得分:0)
这是我们获得子代标记名的方式
xmlData('MyData StudentData Student StudentValue').children().get(4).tagName;