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_userid>444</xg_userid>
  <xg_password>e807f1fcf82d132f9bb018ca6738a19f</xg_password>
  <xg_token />
  <xg_reminder>No reminder has been set! Please contact the system administrator.</xg_reminder>
  <xg_goodsigntime />
  </xg_account>

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

444.xml
----------
<user><UserName>444</UserName>
<DateCreated>2/3/2008 12:30:02 PM</DateCreated>
<CreatedBy>1234</CreatedBy>
<DataAccess></DataAccess>
<ExpiryDate></ExpiryDate>
<ExcludeUser>False</ExcludeUser>
<GroupName>CESUsers</GroupName>
<DateLastUpdated>2/4/2008 10:44:01 AM</DateLastUpdated>
<LastUpdatedBy>1234</LastUpdatedBy></user>

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..

Regards
Sam


 
niidmoreAsked:
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:
Hello,
Can someone help me out in this please? It is quite urgent now.
Many Thanks
Regards
Sam
0
davidlambCommented:
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 http://w3schools.com/xsl/default.asp 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)
  xmlSource.save "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)
  file.Close
 
' end For-Each loop
Next XMLFile

Open in new window

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

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

davidlambCommented:
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

0
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>"
  xmlSource.save iUserId & ".xml"
 
 
  strTransformResult = xmlSource.transformNode(xmlStyle)
  file = fso.OpenTextFile(iUserId & ".xml", 8, true)
  file.WriteLine xmlSource.transformNode(xmlStyle)
  file.Close
 
Next


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
sam


<?xml version="1.0" encoding="iso-8859-1"?>
 
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
  <xsl:template match="/">
    <html>
      <body>
         <table border="1">
          <tr bgcolor="#9acd32">
              <th align="left">user id</th>
          </tr>
          <xsl:for-each select="catalog/cd">
            <tr>
              <td>
                <xsl:value-of select="xg_userid"/>
              </td>
            
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
 
</xsl:stylesheet>

Open in new window

0
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.
Regards
Sam


<%
 
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\'" & f.name  & "'"       
        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
		file.close
		
Next
%>

Open in new window

0
niidmoreAuthor Commented:
Please tell me how I can extract <xg_userid> value from the loaded XML.
0
davidlambCommented:
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
0
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
0
davidlambCommented:
Sam,
Have you confirmed that f.name 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.)
0

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 f.name is not returning any value at all, I am now using MSXML2
Please advice
Regards
Sam
0
davidlambCommented:
Make sure that theFolder.files.length > 0.
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
XML

From novice to tech pro — start learning today.