Posts: 763
Threads: 261
Joined: Jul 2012
If I have the following XML file
<?xml version="1.0" encoding="UTF-8"?>
<snippetcollection>
<maincat name="php">
<subcat name="strings">
<snippet>
<title>explode-example1</title>
<content>txttxttxt</content>
</snippet>
</subcat>
</maincat>
</snippetcollection>
How can I create folders and subfolder with a text file and set it's content:
php (folder, main_cat)
|
|--strings (subfolder, subcat)
|
|--explode-example1.txt (textfile and contents="txttxttxt")
Posts: 12,028
Threads: 139
Joined: Dec 2002
Please give valid XML example. Things like <main_cat="php"> are invalid.
Posts: 763
Threads: 261
Joined: Jul 2012
My apologies for that, I changed the XML example.
Posts: 12,028
Threads: 139
Joined: Dec 2002
Macro Macro313
out
str folder="$desktop$\xml to files"
mkdir folder
str sXml=
;<snippetcollection>
;;<maincat name="php">
;;;;;;<subcat name="strings">
;;;;;;;;;<snippet>
;;;;;;;;;;;;<title>explode-example1</title>
;;;;;;;;;;;;<content>txttxttxt1</content>
;;;;;;;;;</snippet>
;;;;;;;;;<snippet>
;;;;;;;;;;;;<title>explode-example2</title>
;;;;;;;;;;;;<content>txttxttx2t</content>
;;;;;;;;;</snippet>
;;;;;;;</subcat>
;;;;;;<subcat name="strings2">
;;;;;;;;;<snippet>
;;;;;;;;;;;;<title>explode-example3</title>
;;;;;;;;;;;;<content>txttxttxt3</content>
;;;;;;;;;</snippet>
;;;;;;;</subcat>
;;;</maincat>
;</snippetcollection>
IXml x=CreateXml
x.FromString(sXml)
;create folder for <maincat>
IXmlNode nmc=x.Path("snippetcollection/maincat")
str mainFolderName=nmc.AttributeValue("name")
mainFolderName.ReplaceInvalidFilenameCharacters("_")
mkdir F"{folder}\{mainFolderName}"
;get all <subcat>
ARRAY(IXmlNode) a1 a2
nmc.Path("subcat" a1)
int i j
for i 0 a1.len ;;for each <subcat>
,IXmlNode& r1=a1[i]
,str folderName=r1.AttributeValue("name")
,out "---- %s" folderName
,folderName.ReplaceInvalidFilenameCharacters("_")
,mkdir F"{folder}\{mainFolderName}\{folderName}"
,;get all <snippet>
,r1.Path("snippet" a2)
,for j 0 a2.len ;;for each <snippet>
,,IXmlNode& r2=a2[j]
,,str title content
,,title=r2.Child("title").Value
,,content=r2.Child("content").Value
,,out "title='%s', content='%s'" title content
,,title.ReplaceInvalidFilenameCharacters("_")
,,str filePath=F"{folder}\{mainFolderName}\{folderName}\{title}.txt"
,,content.setfile(filePath)
run folder
Posts: 763
Threads: 261
Joined: Jul 2012
Thank you again for your assistance!!!!!!!
Posts: 763
Threads: 261
Joined: Jul 2012
Sorry to bother you again, I really am but this very simple problem is beating me up ( I tried different approaches using the helpfile and xml discussions on forum).
The following example works perfect to extract "mainfolder-1" and "mainfolder-2", they both get printed out in the "out" window.
Macro Macro
str sXml=
;<?xml version = "1.0"?>
;<Folder-list>
,;<Folder>
,,;<SubFolders>
,,,;<Folder>
,,,,;<Expanded>False1b</Expanded>
,,,,;<Snippets></Snippets>
,,,,;<Title>sub1</Title>
,,,;</Folder>
,,;</SubFolders>
,,;<Title>mainfolder-1</Title>
,;</Folder>
,;<Folder>
,,;<SubFolders>
,,,;<Folder>
,,,,;<Expanded>False2b</Expanded>
,,,,;<Snippets></Snippets>
,,,,;<Title>sub2</Title>
,,,;</Folder>
,,;</SubFolders>
,,;<Title>mainfolder-2</Title>
,;</Folder>
;</Folder-list>
IXml x=CreateXml
x.FromString(sXml)
ARRAY(IXmlNode) b
;------cycle path-------------
x.Path("/Folder-list/Folder" b)
;------cycle path-------------
int i
for i 0 b.len
,str titlecheck=b[i].ChildValue("Title")
,,if (!empty(titlecheck))
,,,,out(titlecheck)
But when I want to extract the "sub1" and "sub2", it doesn't work. What I mean is, only 1 gets printed out in the "out" window => "sub1".
Not "sub1" and "sub2". The method I used is by altering:
Macro Macro
;------cycle path-------------
x.Path("/Folder-list/Folder" b)
;------cycle path-------------
to
Macro Macro
;------cycle path-------------
x.Path("/Folder-list/Folder/SubFolders/Folder" b)
;------cycle path-------------
Posts: 12,028
Threads: 139
Joined: Dec 2002
Path() does not support this. When it finds first <SubFolders>, it gets all its children <Folder> and does not search for other <SubFolders>. Standard XPath probably would get all, but QM supports XPath only partially.
In this case need 2 loops.
Macro Macro1797
str sXml=
;<?xml version = "1.0"?>
;<Folder-list>
,;<Folder>
,,;<SubFolders>
,,,;<Folder>
,,,,;<Expanded>False1b</Expanded>
,,,,;<Snippets></Snippets>
,,,,;<Title>sub1</Title>
,,,;</Folder>
,,;</SubFolders>
,,;<Title>mainfolder-1</Title>
,;</Folder>
,;<Folder>
,,;<SubFolders>
,,,;<Folder>
,,,,;<Expanded>False2b</Expanded>
,,,,;<Snippets></Snippets>
,,,,;<Title>sub2</Title>
,,,;</Folder>
,,;</SubFolders>
,,;<Title>mainfolder-2</Title>
,;</Folder>
;</Folder-list>
IXml x=CreateXml
x.FromString(sXml)
ARRAY(IXmlNode) b c
;------cycle path-------------
x.Path("/Folder-list/Folder" b)
;------cycle path-------------
int i j
for i 0 b.len
,str titlecheck=b[i].ChildValue("Title")
,if(!empty(titlecheck))
,,out(titlecheck)
,b[i].Path("SubFolders/Folder" c)
,for j 0 c.len
,,titlecheck=c[j].ChildValue("Title")
,,if(!empty(titlecheck))
,,,out(titlecheck)
,,
Posts: 763
Threads: 261
Joined: Jul 2012
OK!
Thank you very very much!!!!
Posts: 763
Threads: 261
Joined: Jul 2012
I am sorry to bother you again, in the final stages of my macro I can't finish it because of a very "simple" problem.
I am trying to create a directory structure from an array, but I keep getting the error:
Error (RT) in Macro3: cannot create folder: Cannot create a file when that file already exists. ?
I simplified the part where everything stops:
Macro Macro3
out
ARRAY(str) path_string
str drive="d:\test\"
int i
path_string.create(3)
path_string[0]="mainfolder-1\sub-1\sub-sub-1\"
path_string[1]="mainfolder-2\sub-2\"
path_string[2]="mainfolder-3\sub3\sub-sub-3\sub-sub-sub-3\"
;path_string[0]="d:\test\mainfolder-1\sub-1\sub-sub-1\"
;path_string[1]="d:\test\mainfolder-2\sub-2\"
;path_string[2]="d:\test\mainfolder-3\sub3\sub-sub-3\sub-sub-sub-3\"
;
for i 0 path_string.len
,out "%i %s" i path_string[i]
,mkdir F"{drive}{path_string[i]}"
I am guessing that when the second array index is beeing processed, it halts because "d:\test" already exists? (the green 'commented out' block was another attempt where I incorporated the whole path, but it didn't work either)
Is there any way around this? The "mkdir" command hasn't got any special flags (overwrite?) I can use.
The final form of the code will be sometehing like this
Macro xml_folder_structure_builder
str filePath=F"d:\test\{folder_stucture[m1i]}\{snippet_title}.txt"
snippet_content.setfile(filePath)
{folder_structure[m1i]} and {snippet_title} will be retrieved from an array
Posts: 12,028
Threads: 139
Joined: Dec 2002
It seems that mkdir does not like \ at the end of path.
I'll fix it in next QM.
Posts: 763
Threads: 261
Joined: Jul 2012
|