Jump to content
Sign in to follow this  
gillesg

splitting xml file using XMLDOM

Recommended Posts

gillesg

Hi all,

Once again I am stuck using XMLDOM.

Let me explain you what I try to achieve :

I have an XML file that I want to split in multiple smaller file based on certain criterias.

Below My xml file (also attached)

<KeyFileAssoc version="0.3">
<Config>
<AppsRoot>..\..\Apps</AppsRoot>
<Language file="KeyFileAssoc_fr.xml">fr</Language>
<UpdateAutoCheck>on</UpdateAutoCheck>
<SysTrayFastSwitch>on</SysTrayFastSwitch>
<ShowAssocToolTip>on</ShowAssocToolTip>
<UseLbkProxySettings>on</UseLbkProxySettings>
</Config>
<AppsAssoc>
<TypeDescription.en>URL : LiberKey Installation Protocol (lbkinst)</TypeDescription.en>
<TypeDescription.fr>URL : Protocole d&apos;installation LiberKey (lbkinst)</TypeDescription.fr>
<TypeIcon index="0">%LBKROOT%\LiberKeyTools\LKAppsVCheck\LKAppsVCheck.exe</TypeIcon>
<Shell default="open">
<Action name="open">
     <ExePath>%LBKROOT%\LiberKeyTools\LKAppsVCheck\LKAppsVCheck.exe</ExePath>
     <CmdArgs>%1</CmdArgs>
     <Description.en>Install on my LiberKey</Description.en>
     <Description.fr>Installer sur ma LiberKey</Description.fr>
     <AppName>LiberKeyOnlineSetup</AppName>
</Action>
</Shell>
</FileType>
<FileType ext="mp3">
<TypeDescription.en>MPEG Audio Stream file, Layer III (MP3)</TypeDescription.en>
<TypeDescription.fr>Fichier MPEG Audio Stream, Layer III (MP3)</TypeDescription.fr>
<Shell>
<Action name="ouvrir_avec_mp3tag__liberkey">
     <Description.en>Ouvrir avec Mp3tag (LiberKey)</Description.en>
     <Description.fr>Ouvrir avec Mp3tag (LiberKey)</Description.fr>
     <ExePath>%APPS%\Mp3tag\Mp3tagLKL.exe</ExePath>
     <AppName>Mp3tag</AppName>
     <CmdArgs>&quot;%1&quot;</CmdArgs>
</Action>
<Action name="vuplayer">
     <Description.en>Open with AIMP (LiberKey)</Description.en>
     <Description.fr>Ouvrir avec AIMP (LiberKey)</Description.fr>
     <ExePath>%APPS%\AIMP\AIMPLKL.exe</ExePath>
     <AppName>AIMP</AppName>
     <CmdArgs>&quot;%1&quot;</CmdArgs>
</Action>
<Action name="editer_avec_audacity__liberkey">
     <Description.en>Edit with Audacity (LiberKey)</Description.en>
     <Description.fr>Editer avec Audacity (LiberKey)</Description.fr>
     <ExePath>%APPS%\Audacity\AudacityLKL.exe</ExePath>
     <AppName>Audacity </AppName>
     <CmdArgs>&quot;%1&quot;</CmdArgs>
</Action>
</Shell>
</FileType>
<FileType ext="wav">
<TypeDescription.en>Audio file (WAV)</TypeDescription.en>
<TypeDescription.fr>Fichier Audio (WAV)</TypeDescription.fr>
<Shell>
<Action name="vuplayer">
<Description.fr>Ouvrir avec AIMP (LiberKey)</Description.fr>
     <ExePath>%APPS%\AIMP\AIMPLKL.exe</ExePath>
     <AppName>AIMP</AppName>
     <CmdArgs>&quot;%1&quot;</CmdArgs>
</Action>
<Action name="editer_avec_audacity__liberkey">
     <Description.en>Edit with Audacity (LiberKey)</Description.en>
     <Description.fr>Editer avec Audacity (LiberKey)</Description.fr>
     <ExePath>%APPS%\Audacity\AudacityLKL.exe</ExePath>
     <AppName>Audacity </AppName>
     <CmdArgs>&quot;%1&quot;</CmdArgs>
</Action>
</Shell>
</FileType>
</AppsAssoc>
<AppsAssocErrors/>
</KeyFileAssoc>

I want, based on this xml file to create 3 xml file containning information in relation to the AppName Value.

So far I try to read my file and display what I need to write in different file after.

My actual code

#include <File.au3>
#Include <array.au3>
#include "_XMLDomWrapper.au3"

if $CmdLine[0] < 1 Then
msgbox(0,'','usage : ' & @CRLF & @TAB & @ScriptName & ' KfaXMLFile.xml')
EndIf
if FileExists($CmdLine[1]) Then
Explode_KFA($CmdLine[1])
Else
msgbox (0,'','usage : ' & @CRLF & @TAB & @ScriptName & ' KfaXMLFile.xml' & @CRLF & _
@TAB & ' KfaXMLFile.xml must be a valid KFA XML file')
EndIf
Exit

Func Explode_KFA($KFAFile)
_XMLFileOpen($KFAFile)
$strXpathBase = '//KeyFileAssoc/AppsAssoc/FileType[@ext]'

$aGetValue = _XMLGetValue($strXpathBase & '/Shell/Action/AppName')
$aUniq=_ArrayUnique($aGetValue, 1,1,1)

_ArrayDisplay($aUniq, ' Initial ')

For $i=1 to $aUniq[0]
; Get list of XMLTAG
$aXmlTag=_ArrayUnique( _XMLSelectNodes($strXpathBase & '/Shell/Action[AppName[starts-with(text(),"'& $aUniq[$i] & '")]]/*'), 1,1,1)
_ArrayDisplay($aXmlTag,'Field : ' & $aUniq[$i])

$aXmlNodes=_XMLSelectNodes($strXpathBase & '/Shell/Action[AppName[starts-with(text(),"'& $aUniq[$i] & '")]]')
_ArrayDisplay($aXmlNodes,'Nodes')
For $iCurrentNode = 1 to $aXmlNodes[0]
For $iNode = 1 to $aXmlTag[0]
$aValueExe = _XMLGetValue($strXpathBase & '/Shell/Action['&$iCurrentNode&'][AppName[starts-with(text(),"'& $aUniq[$i] & '")]]/' & $aXmlTag[$iNode])
_ArrayDisplay($aValueExe,$aXmlTag[$iNode] & ' : ' & $aUniq[$i])
Next
Next
Next
EndFunc

There I am stuck !

I don't know how to get for AppName = AIMP the different values, knowing that 1 entry is missing.

For <Action name="vuplayer">, there is no <Description.en>.

Can anyone give me some directions/hints to look into.

I am kind lost there.

Based on what I want to achieve, may be there is something more simple. If so feel free.

Regards.

Gilles

SplitXMLfile.au3

KeyFileAssoc.xml

Share this post


Link to post
Share on other sites
jdelaney

Here, i'm looping through the file types, and dynamically picking out the App Names...you can build upon it to create arrays of the data you need

$string = '<KeyFileAssoc version="0.3">' & @crlf & _
'<Config>' & @crlf & _
'<AppsRoot>....Apps</AppsRoot>' & @crlf & _
'<Language file="KeyFileAssoc_fr.xml">fr</Language>' & @crlf & _
'<UpdateAutoCheck>on</UpdateAutoCheck>' & @crlf & _
'<SysTrayFastSwitch>on</SysTrayFastSwitch>' & @crlf & _
'<ShowAssocToolTip>on</ShowAssocToolTip>' & @crlf & _
'<UseLbkProxySettings>on</UseLbkProxySettings>' & @crlf & _
'</Config>' & @crlf & _
'<AppsAssoc>' & @crlf & _
'<FileType ext="mp3">' & @crlf & _
'<TypeDescription.en>MPEG Audio Stream file, Layer III (MP3)</TypeDescription.en>' & @crlf & _
'<TypeDescription.fr>Fichier MPEG Audio Stream, Layer III (MP3)</TypeDescription.fr>' & @crlf & _
'<Shell>' & @crlf & _
'<Action name="ouvrir_avec_mp3tag__liberkey">' & @crlf & _
'    <Description.en>Ouvrir avec Mp3tag (LiberKey)</Description.en>' & @crlf & _
'    <Description.fr>Ouvrir avec Mp3tag (LiberKey)</Description.fr>' & @crlf & _
'    <ExePath>%APPS%Mp3tagMp3tagLKL.exe</ExePath>' & @crlf & _
'    <AppName>Mp3tag</AppName>' & @crlf & _
'    <CmdArgs>&quot;%1&quot;</CmdArgs>' & @crlf & _
'</Action>' & @crlf & _
'<Action name="vuplayer">' & @crlf & _
'    <Description.en>Open with AIMP (LiberKey)</Description.en>' & @crlf & _
'    <Description.fr>Ouvrir avec AIMP (LiberKey)</Description.fr>' & @crlf & _
'    <ExePath>%APPS%AIMPAIMPLKL.exe</ExePath>' & @crlf & _
'    <AppName>AIMP</AppName>' & @crlf & _
'    <CmdArgs>&quot;%1&quot;</CmdArgs>' & @crlf & _
'</Action>' & @crlf & _
'<Action name="editer_avec_audacity__liberkey">' & @crlf & _
'    <Description.en>Edit with Audacity (LiberKey)</Description.en>' & @crlf & _
'    <Description.fr>Editer avec Audacity (LiberKey)</Description.fr>' & @crlf & _
'    <ExePath>%APPS%AudacityAudacityLKL.exe</ExePath>' & @crlf & _
'    <AppName>Audacity </AppName>' & @crlf & _
'    <CmdArgs>&quot;%1&quot;</CmdArgs>' & @crlf & _
'</Action>' & @crlf & _
'</Shell>' & @crlf & _
'</FileType>' & @crlf & _
'<FileType ext="wav">' & @crlf & _
'<TypeDescription.en>Audio file (WAV)</TypeDescription.en>' & @crlf & _
'<TypeDescription.fr>Fichier Audio (WAV)</TypeDescription.fr>' & @crlf & _
'<Shell>' & @crlf & _
'<Action name="vuplayer">' & @crlf & _
'<Description.fr>Ouvrir avec AIMP (LiberKey)</Description.fr>' & @crlf & _
'    <ExePath>%APPS%AIMPAIMPLKL.exe</ExePath>' & @crlf & _
'    <AppName>AIMP</AppName>' & @crlf & _
'    <CmdArgs>&quot;%1&quot;</CmdArgs>' & @crlf & _
'</Action>' & @crlf & _
'<Action name="editer_avec_audacity__liberkey">' & @crlf & _
'    <Description.en>Edit with Audacity (LiberKey)</Description.en>' & @crlf & _
'    <Description.fr>Editer avec Audacity (LiberKey)</Description.fr>' & @crlf & _
'    <ExePath>%APPS%AudacityAudacityLKL.exe</ExePath>' & @crlf & _
'    <AppName>Audacity </AppName>' & @crlf & _
'    <CmdArgs>&quot;%1&quot;</CmdArgs>' & @crlf & _
'</Action>' & @crlf & _
'</Shell>' & @crlf & _
'</FileType>' & @crlf & _
'</AppsAssoc>' & @crlf & _
'<AppsAssocErrors/>' & @crlf & _
'</KeyFileAssoc>'
$oXML=ObjCreate("Microsoft.XMLDOM")
$oXML2=ObjCreate("Microsoft.XMLDOM")
$oXML.loadxml($string) ; load document
;ConsoleWrite ( $oXML.xml & @CRLF)
$result = $oXML.selectNodes( '//FileType' )
For $Node In $result ; (might need $iUBound-1, not sure if this is 0 based...probably 1 based)
$att = $Node.selectSingleNode ( "@ext" )
; Loop through each file type
$oXML2.loadxml($Node.xml)
$result2 = $oXML2.selectNodes( '//Action' )
; Loop through apps for each file type
For $Node2 In $result2
$attValue2 = $Node2.selectSingleNode ( "@name" )
$AppName = $Node2.selectSingleNode ( "//AppName" )
ConsoleWrite ( "FileType=[" & $att.text & "] AppName=[" & $attValue2.value & "]" & @CRLF )
Next
Next

Example of stripping out each child node of each //Action

$result = $oXML.selectNodes( '//FileType' )
For $Node In $result
 $att = $Node.selectSingleNode ( "@ext" )
 ; Loop through each file type
 $oXML2.loadxml($Node.xml)
 $result2 = $oXML2.selectNodes( '//Action' )
 ; Loop through apps for each file type
;~  For $Node2 In $result2
;~   $attValue2 = $Node2.selectSingleNode ( "@name" )
;~   $AppName = $Node2.selectSingleNode ( "//AppName" )
;~   ConsoleWrite (  "FileType=[" & $att.text & "] AppName=[" & $attValue2.value & "]" &  @CRLF )
;~  Next
 $iCounter = 1
 For $Node2 In $result2
  $children = $Node2.childnodes
  For $child in $children
   ConsoleWrite (  "FileType=[" & $att.text & "] GroupInstance=[" & $iCounter & "] childNode|value=[" & $child.nodename & "|" & $child.text & "]" &  @CRLF )
  Next
  $iCounter +=1
 Next
 ConsoleWrite ( @CRLF )
Next

will spit out:

FileType=[mp3] GroupInstance=[1] childNode|value=[Description.en|Ouvrir avec Mp3tag (LiberKey)]

FileType=[mp3] GroupInstance=[1] childNode|value=[Description.fr|Ouvrir avec Mp3tag (LiberKey)]

FileType=[mp3] GroupInstance=[1] childNode|value=[ExePath|%APPS%Mp3tagMp3tagLKL.exe]

FileType=[mp3] GroupInstance=[1] childNode|value=[AppName|Mp3tag]

FileType=[mp3] GroupInstance=[1] childNode|value=[CmdArgs|"%1"]

FileType=[mp3] GroupInstance=[2] childNode|value=[Description.en|Open with AIMP (LiberKey)]

FileType=[mp3] GroupInstance=[2] childNode|value=[Description.fr|Ouvrir avec AIMP (LiberKey)]

FileType=[mp3] GroupInstance=[2] childNode|value=[ExePath|%APPS%AIMPAIMPLKL.exe]

FileType=[mp3] GroupInstance=[2] childNode|value=[AppName|AIMP]

FileType=[mp3] GroupInstance=[2] childNode|value=[CmdArgs|"%1"]

FileType=[mp3] GroupInstance=[3] childNode|value=[Description.en|Edit with Audacity (LiberKey)]

FileType=[mp3] GroupInstance=[3] childNode|value=[Description.fr|Editer avec Audacity (LiberKey)]

FileType=[mp3] GroupInstance=[3] childNode|value=[ExePath|%APPS%AudacityAudacityLKL.exe]

FileType=[mp3] GroupInstance=[3] childNode|value=[AppName|Audacity]

FileType=[mp3] GroupInstance=[3] childNode|value=[CmdArgs|"%1"]

FileType=[wav] GroupInstance=[1] childNode|value=[Description.fr|Ouvrir avec AIMP (LiberKey)]

FileType=[wav] GroupInstance=[1] childNode|value=[ExePath|%APPS%AIMPAIMPLKL.exe]

FileType=[wav] GroupInstance=[1] childNode|value=[AppName|AIMP]

FileType=[wav] GroupInstance=[1] childNode|value=[CmdArgs|"%1"]

FileType=[wav] GroupInstance=[2] childNode|value=[Description.en|Edit with Audacity (LiberKey)]

FileType=[wav] GroupInstance=[2] childNode|value=[Description.fr|Editer avec Audacity (LiberKey)]

FileType=[wav] GroupInstance=[2] childNode|value=[ExePath|%APPS%AudacityAudacityLKL.exe]

FileType=[wav] GroupInstance=[2] childNode|value=[AppName|Audacity]

FileType=[wav] GroupInstance=[2] childNode|value=[CmdArgs|"%1"]

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
MvL

Hi GillesG,

I wrote this XML splitter.

Does this solve your problem ?

The call is:

Global $showResults, $sContent, $strZoek

Global $arrLevel[1], $arrSpaces[1], $arrNode[1], $arrResult[1]

$sContent = '<brp:aanschrijving><brp:voornamen>Bennie</brp:voornamen></brp:aanschrijving>

$strZoek = 'brp:voornamen'

$showResults = True

TuXmlParse($sContent, $strZoek, $arrLevel, $arrSpaces, $arrNode, $arrResult, $showResults)

35_Tuned XML.au3

This results in:

[1]||1|.x|<brp:aanschrijving>|Bennie

Share this post


Link to post
Share on other sites
MvL

For

$sContent = '<brp:aanschrijving><brp:voornamen>Bennie</brp:voornamen></brp:aanschrijving><brp:aanschrijving><brp:voornamen>Adam</brp:voornamen></brp:aanschrijving>'

$strZoek = 'brp:aanschrijving'

the result that will be shown is:

[0] 2 levels|2 inspringingen|2 nodes|2 results voor <brp:aanschrijving>

[1] 0|x|<<>|<brp:voornamen>Bennie</brp:voornamen>

[2]||0|x|<<>|<brp:voornamen>Adam</brp:voornamen>

And the content of: $arrResult[] is

[0]|2

[1]|<brp:voornamen>Bennie</brp:voornamen>

[2]|<brp:voornamen>Adam</brp:voornamen>

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×