read xml data from the folder and write it back - ASP

Hello All,

I have 400+ XML files in the following format

<?xml version="1.0" ?>
- <xg_account>
  <xg_token />
  <xg_reminder>No reminder has been set! Please contact the system administrator.</xg_reminder>
  <xg_goodsigntime />

I would need to read all these files and write it back to some other folder in the format below.

Task, is to read <xg_userid> node, put it int the <Username> node and give the file as <Username>.xml

<DateCreated>2/3/2008 12:30:02 PM</DateCreated>
<DateLastUpdated>2/4/2008 10:44:01 AM</DateLastUpdated>

I probably have not explained you well, if you need any more clarifications please let me know soon.

your help on this is much appreciated..


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

niidmoreAuthor Commented:
Can someone help me out in this please? It is quite urgent now.
Many Thanks
1. Create an XSL stylesheet (e.g., stylesheet.xsl) for the appropriate transformation.
2. Create a VBSscript (.vbs) file to individually read in each file, transform the XML using the XSL/XSLT, and saves the file.

If you're not familiar with XSL, go to for an excellent tutorial. (Your needs are fairly simple, so the tutorial should apply pretty well.)

Here's some preliminary untested code. In the example below, xmlSource.transformNode simply returns a string.
Dim xmlSource, xmlStyle, strTransformResult, fso, file
Set xmlSource	= WScript.CreateObject("MSXML2.DOMDocument.4.0")
Set xmlStyle	= WScript.CreateObject("MSXML2.DOMDocument.4.0")
Set fso		= WScript.CreateObject("Scripting.FileSystemObject")
' For each XML file
  xmlSource.load "inputFile.xml"
  xmlStyle.load "stylesheet.xsl"
  strTransformResult = xmlSource.transformNode(xmlStyle)
  ' Saving Option #1 (Preferred, if it works as I expect):
  xmlSource.loadXML xmlSource.transformNode(xmlStyle) "outputFile.xml"
  ' Saving Option #2 (have to use FileSystemObject)
  strTransformResult = xmlSource.transformNode(xmlStyle)
  file = fso.OpenTextFile("outputFile.xml", 8, true)
  file.WriteLine xmlSource.transformNode(xmlStyle)
' end For-Each loop
Next XMLFile

Open in new window

And, of course, if you're doing this in ASP, change WScript.CreateObject to Server.CreateObject. Otherwise, everything should be the same.
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Also, I just noticed that a lot of the stuff in your output XML isn't part of your input. I assume you'll have access to that extra data in your ASP code. Try this instead inside your for-each loop:
(P.S., load your stylesheet BEFORE starting the loop. There's no need to keep reloading the same file.)
xmlSource.loadXML "inputFile.xml"
xmlSource.loadXML xmlSource.transformNode(xmlStyle)
// for each element whose contents must be provided by the code.
xmlSource.documentElement.appendChild "GroupName", strGroupName
xmlSource.saveFile "outputFile.xml"

Open in new window

niidmoreAuthor Commented:
Hello David,
Thanks for your time in working this out for me, I have followed your suggestion and this is what I have in the code which doesnt seems to be working well..

Dim xmlSource, xmlStyle, strTransformResult, fso, file
Set xmlSource   = Server.CreateObject("MSXML2.DOMDocument.4.0")
Set xmlStyle    = Server.CreateObject("MSXML2.DOMDocument.4.0")
Set fso         = Server.CreateObject("Scripting.FileSystemObject")

set theFolder = fso.getFolder("e:\tbp\xygnin-z\ces\access\data\users")
for Each f In theFolder.files          ' For each XML file
  xmlSource.load f
  xmlStyle.load "e:\tbp\xygnin-z\ces\stylesheet.xsl"
  strTransformResult = xmlSource.transformNode(xmlStyle)
  xmlSource.loadXML xmlSource.transformNode(xmlStyle)
  xmlSource.documentElement.appendChild "Username", strGroupName
  iUserId = "<user id from previous xml file>" iUserId & ".xml"
  strTransformResult = xmlSource.transformNode(xmlStyle)
  file = fso.OpenTextFile(iUserId & ".xml", 8, true)
  file.WriteLine xmlSource.transformNode(xmlStyle)

I need to get the userId from the previous xml file and assign that as a file name, hope you see what my point is .....

many thanks

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
  <xsl:template match="/">
         <table border="1">
          <tr bgcolor="#9acd32">
              <th align="left">user id</th>
          <xsl:for-each select="catalog/cd">
                <xsl:value-of select="xg_userid"/>

Open in new window

niidmoreAuthor Commented:
Hello David,
If you are still around, please see the script below. I think I have almost got what I would need. However, I am now getting object required error line # 18.
Please have a look at this.

Dim xmlSource, fso, file, uname
Set xmlSource   = Server.CreateObject("Microsoft.XMLDOM")
Set fso         = Server.CreateObject("Scripting.FileSystemObject")
set theFolder = fso.getFolder("e:\account")
for Each f In theFolder.files       
        xmlSource.load "e:\account\'" &  & "'"       
        uname = xmlSource.selectSingleNode("xg_userid").text 
        s = s & "<user><DateCreated/><CreatedBy/><DataAccess/><ExpiryDate/>"
        s = s & "<ExcludeUser>False</ExcludeUser>"
        s = s & "<Username>" & uname & "</Username>"
        s = s & "<GroupName>CESUsers</GroupName>"
        s = s & "<DateLastUpdated>1/31/2008 4:06:30 PM</DateLastUpdated>"
        s = s & "<LastUpdatedBy>1234</LastUpdatedBy></user>"
        filename = uname & ".xml" 
        set file = fso.CreateTextFile("e:\users\" & filename) '"
		file.write s

Open in new window

niidmoreAuthor Commented:
Please tell me how I can extract <xg_userid> value from the loaded XML.
Two things.
1) "Microsoft.XMLDom" is outdated. Use "MSXML2.DOMDocument" if at all possible. (You don't need to use the version numbers if you don't want to.)
2) uname = xmlSource.documentElement.selectSingleNode("xg_userid").text
niidmoreAuthor Commented:
Hello David,
Thanks for your suggestions I will try to use msxml2 in the future.
I still does not seems to have my code working as desired, it is not loading the files I am picking from the folder and so is the reason I am getting object required error at

uname = xmlSource.documentElement.selectSingleNode("xg_userid").text
Have you confirmed that has the value that you're expecting?
Try running xmlSource.load by explicitly specifying the entire path (including filename) to confirm that you can do that. (I don't think it's the case, but it's entirely possible the problem is with Microsoft.XMLDOM. I only know that my suggestion will work for MSXML2, and can't vouch for the other.)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
niidmoreAuthor Commented:
Hello David,
Are you still here? I am sorry for not getting back to you any sooner on this as I was been occupied elsewhere.
No, the is not returning any value at all, I am now using MSXML2
Please advice
Make sure that theFolder.files.length > 0.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.