Expand my Community achievements bar.

SOLVED

Extract and save xml nodes to filesystem

Avatar

Level 4

Hi All,

I am looking for great ideas for the best way to extract xml nodes from a complete xml-file and save each to a given path in the filesystem, with filename incremented for each file... file1,2,3,4.

I have a process in workbench that takes an xml file as input (complete XML shown below). In the complete solution I will have an unknown number of child nodes to save.

Complete XML file with two records. Is in a process variable "xmlComplete" - type=xml:

==================

<archiveIndexRecords>
     <indexRec>
          <i1>7173</i1>
          <i2>1405781111</i2>
          <i3>71709999999</i3>
     </indexRec>
     <indexRec>
          <i1>6226</i1>
          <i2>1405782222</i2>
          <i3>71708888888</i3>
     </indexRec>
</archiveIndexRecords>

Want to save each child record (<indexRec>) to the filesystem. Lets say to C:\temp\records\file1.xml   (..2.xml etc) and with content like this.

<indexRec>
     <i1>7173</i1>
     <i2>1405781111</i2>
     <i3>71709999999</i3>
</indexRec>

...

What kind of solution would you advice?? An execute script with loop or a loop of setValue steps??

Hope for great input. feel free to ask for more info if needed, Thanks

Yours,

Thomas Groenbaek

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

Hi,

You need to create a loop in your process on workbench. First count nodes and set it to an integer variable.

/process_data/@number = count(/process_data/xmlComplete/archiveIndexRecords/indexRec)

Then you need to get first node's data to write to disk.

/process_data/FirstNode = /process_data/xmlComplete/archiveIndexRecords/indexRec[number(/process_data/@i)] (i is another integer variable which is 1 as default. )

When you get first node's data set it to a document variable and write down to disk. Then add 1 to i variable and control it with number (count of nodes) variable. Decide for next record or not.

You can find an example how to create loops in workbench link below.

http://muratkuru.com.tr/?p=106

Murat

www.muratkuru.com.tr

View solution in original post

2 Replies

Avatar

Level 10

Your requirement is much easy to implement with simple activities such as SetValue, WriteXML etc.

I prefer not to use Execute Script activity for this simple logic.

Nith

Avatar

Correct answer by
Former Community Member

Hi,

You need to create a loop in your process on workbench. First count nodes and set it to an integer variable.

/process_data/@number = count(/process_data/xmlComplete/archiveIndexRecords/indexRec)

Then you need to get first node's data to write to disk.

/process_data/FirstNode = /process_data/xmlComplete/archiveIndexRecords/indexRec[number(/process_data/@i)] (i is another integer variable which is 1 as default. )

When you get first node's data set it to a document variable and write down to disk. Then add 1 to i variable and control it with number (count of nodes) variable. Decide for next record or not.

You can find an example how to create loops in workbench link below.

http://muratkuru.com.tr/?p=106

Murat

www.muratkuru.com.tr

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----