Reputation: 251
Doing a VBScript that should parse XML and get some info, here's an example of XML:
<?xml version="1.0" encoding="windows-1251"?>
<Performance>
<Group>EX-007
<Student>
<Name>testname</Name><ID>12345</ID>
<Subject>Informatics
<Semester>1<Mark>
<Exam>5</Exam>
</Mark></Semester></Subject>
<Subject>Phys
<Semester>2<Mark>
<PersonalTask>4</PersonalTask>
<Exam>3</Exam>
</Mark></Semester></Subject>
</Student>
</Group>
</Performance>
This is the VBScript code:
Set xml = CreateObject("Msxml.DOMDocument")
success = xml.Load("data1.xml")
Set root = xml.DocumentElement
Set nodes = root.SelectNodes("Group")
WScript.Echo(nodes(0).text)
I would like to get an "EX-007" output in console, but instead it outputs the whole tree under the group tag. What's wrong here?
Upvotes: 0
Views: 2167
Reputation: 200203
Your XML is not well-designed, because some of your nodes contain both text and child nodes. You could work around that by replacing the text from the child node(s):
Set re = New RegExp
re.Pattern = "^\s+|\s+$"
For Each node In xml.SelectNodes("//Group")
txt = Replace(node.text, node.SelectSingleNode("./Student").text, "")
WScript.Echo re.Replace(txt)
Next
However, a better solution would be to structure your XML data properly, e.g. by moving the nested text to attributes of their node:
<?xml version="1.0" encoding="windows-1251"?>
<Performance>
<Group Name="EX-007">
<Student>
<Name>testname</Name>
<ID>12345</ID>
<Subject Title="Informatics">
<Semester Number="1">
<Mark>
<Exam>5</Exam>
</Mark>
</Semester>
</Subject>
<Subject Title="Phys">
<Semester Number="2">
<Mark>
<PersonalTask>4</PersonalTask>
<Exam>3</Exam>
</Mark>
</Semester>
</Subject>
</Student>
</Group>
</Performance>
so that you can select the information directly, like this:
For Each node In xml.SelectNodes("//Group/@Name")
WScript.Echo node.text
Next
Upvotes: 1