Want to combined data of all XML files located in a folder in 1 text file

Joined
May 30, 2013
Messages
4
Reaction score
0
Points
1
Hi:



I got a small project. I'm getting multiple folder from the client containing a small XML file in each. The XML file looks like:



<?xml version="1.0" encoding="UTF-8"?>

<Metadataobject>

<from>[email protected]</from>

<jobname>P2_WT1304g_vier_elemente</jobname>

<priority>2</priority>

<timezone>CEST</timezone>

<year>2013</year>

<month>5</month>

<day>29</day>

<hour>14</hour>

</Metadataobject>



I'm trying to fetch and combine the data from multiple XML files to a single text file and send it as attachment from Microsoft Outlook.



I tried something but can't do:



set XMLFile to ((choose file) as string)

set documentName to XMLFile & ".txt" as text

set folderPath to "Volumes:Macintosh HD:Users:imac3:Desktop:XML Test:"

tell application "System Events"

tell XML element 1 of contents of XML file XMLFile

set myMail to (value of (XML elements whose name is "from")) as string

set myJob to (value of (XML elements whose name is "jobname")) as string

set myPriority to (value of (XML elements whose name is "priority")) as string

set myYear to (value of (XML elements whose name is "year")) as string

set myMonth to (value of (XML elements whose name is "month")) as string

set myDay to (value of (XML elements whose name is "day")) as string

set myHour to (value of (XML elements whose name is "hour")) as string

end tell

end tell



tell application "TextEdit"

make new document

set txtMail to "From: " & " " & myMail & "

"

set txtmyJob to "Job Name: " & " " & myJob & "

"

set txtMyPriority to "Priority: " & " " & myPriority & "

"

set txtMyDate to "Date: " & " " & myDay & "/" & myMonth & "/" & myYear & "

"

set txtHour to "Hour: " & " " & myHour

set text of document 1 to txtMail as text

make new word at end of document 1 with data txtmyJob & txtMyPriority & txtMyDate & txtHour

--save document 1 in file ("Volumes/Macintosh HD/Users/imac3/Desktop/XML Test/Test1.txt")

save document 1 in file (folderPath & documentName)

close window

quit

end tell
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
AppleScript really isn't the best tool here. Your best bet is another scripting language, perhaps Python, Perl or Ruby. An easier method though might be to simply use the cat tool (see here).
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,762
Reaction score
2,100
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
Unless I'm oversimplifying it, wouldn't something like this work?
Code:
find . -name "*.xml" | xargs cat >> </path/to/final/XML/file>

Run that in the top level directory that contains all the .XML files (or whatever the extension happens to be) and then cat with the append (>>) all of those individual files to one final one..
 
OP
M
Joined
May 30, 2013
Messages
4
Reaction score
0
Points
1
AppleScript really isn't the best tool here. Your best bet is another scripting language, perhaps Python, Perl or Ruby. An easier method though might be to simply use the cat tool (see here).
Thanks but I'm less than NOVICE in all these languages...
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
Did you take a look at the tutorial I linked to about using the cat tool (or Raz0rEdge's post above)?
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top