Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parse xml to create multiple text files text file

Status
Not open for further replies.

concep86

Programmer
Aug 27, 2002
1
US
I have an xml file that looks like this


<?xml version=&quot;1.0&quot;?>
<Users>
<Message>
<type>Success</type>
<StudentId>123456</StudentId>
<UserName>cpeters</UserName>
<MessageCode>0</MessageCode>
<Section_Number>111</Section_Number>
</Message>
<Message>
<type>Failure</type>
<StudentId>456733</StudentId>
<UserName>jmartin</UserName>
<MessageCode>0</MessageCode>
<Section_Number>129</Section_Number>
</Message>
<Message>
<type>Success</type>
<StudentId>764523</StudentId>
<UserName>jhart</UserName>
<MessageCode>0</MessageCode>
<Section_Number>149</Section_Number>
</Message>
</Users>

and I parse it using the following code....


set xmlDoc=CreateObject(&quot;Microsoft.XMLDOM&quot;)
xmlDoc.async=&quot;false&quot;
xmlDoc.load(&quot;TheAmericanCollege.xml&quot;)
dim intnumber, intnumberFail
intnumber=0
intnumberFail=0
for each x in xmlDoc.documentElement.childNodes
if (x.selectSingleNode(&quot;type&quot;).firstChild.nodeValue=&quot;Success&quot;) then
intnumber=intnumber+1
elseif (x.selectSingleNode(&quot;type&quot;).firstChild.nodeValue=&quot;Failure&quot;) then
intnumberFail=intnumberFail+1
end if
next
arraysiz=intnumber-1
arraysizfail=intnumberFail-1

redim arraystudent(arraysiz,3)
redim arraystudentfail(arraysizfail,3)

for each y in xmlDoc.documentElement.childNodes
if (y.selectSingleNode(&quot;type&quot;).firstChild.nodeValue=&quot;Success&quot;) then
arraystudent(arraysiz,0)=y.selectSingleNode(&quot;UserName&quot;).firstChild.nodeValue
arraystudent(arraysiz,1)=y.selectSingleNode(&quot;Section_Number&quot;).firstChild.nodeValue
arraystudent(arraysiz,2)=y.selectSingleNode(&quot;StudentId&quot;).firstChild.nodeValue
arraystudent(arraysiz,3)=y.selectSingleNode(&quot;MessageCode&quot;).firstChild.nodeValue
arraysiz=arraysiz-1
elseif (y.selectSingleNode(&quot;type&quot;).firstChild.nodeValue=&quot;Failure&quot;) then
arraystudentfail(arraysizfail,0)=y.selectSingleNode(&quot;UserName&quot;).firstChild.nodeValue
arraystudentfail(arraysizfail,1)=y.selectSingleNode(&quot;Section_Number&quot;).firstChild.nodeValue
arraystudentfail(arraysizfail,2)=y.selectSingleNode(&quot;StudentId&quot;).firstChild.nodeValue
arraystudentfail(arraysizfail,3)=y.selectSingleNode(&quot;MessageCode&quot;).firstChild.nodeValue
arraysizfail=arraysizfail-1
end if
next



Problem is now my file is being changed.... to

<?xml version=&quot;1.0&quot;?>
<Users>
<Message>
<type>Success</type>
<StudentId>123456</StudentId>
<UserName>cpeters</UserName>
<MessageCode>0</MessageCode>
<Section_Number>111</Section_Number>
</Message>
<Message>
<type>Failure</type>
<StudentId>456733</StudentId>
<UserName>jmartin</UserName>
<MessageCode>0</MessageCode>
<Section_Number>129</Section_Number>
</Message>
<Message>
<type>Success</type>
<Section>102</Section>
<client_section_id></client_section_id>
<course_id>325</course_id>
<MessageCode>0</MessageCode>
</Message>
</Users>


The problem is that nowthe file changed from all users to sections as well...

I need to be able to identify whether the seconfd node is Scetion or StudentId.

How can I do this...
 
Why not do an if check,
if selectSingleNode(&quot;Section&quot;) is null you have a studentid, otherwise you have a Section. If there is a possibility of having both (one empty one filled) then check the length of the contents next to determine which to use.
-Tarwn ------------ My Little Dictionary ---------
Reverse Engineering - The expensive solution to not paying for proper documentation
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top