Sign in to follow this  
Followers 0
lsakizada

IniToXML - how to?

5 posts in this topic

#1 ·  Posted (edited)

How to convert INI file to XML file format.

Let say we have this INI:

[BSC1]
gatewayId=1
name=SiemensX25
[BSC2]
gatewayId=2
name=SiemnsTCPIP

How to convert it to:

<BSC1>
         <gatewayId>1</gatewayId>
         <name>SiemensX25</name>
</BSC1>
<BSC2>
         <gatewayId>2</gatewayId>
         <name>SiemnsTCPIP</name>
</BSC2>
Edited by lsakizada

Be Green Now or Never (BGNN)!

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

How to convert INI file to XML file format.

Let say we have this INI:

[BSC1]
gatewayId=1
name=SiemensX25
[BSC2]
gatewayId=2
name=SiemnsTCPIP

How to convert it to:

<BSC1>
         <gatewayId>1</gatewayId>
         <name>SiemensX25</name>
</BSC1>
<BSC2>
         <gatewayId>2</gatewayId>
         <name>SiemnsTCPIP</name>
</BSC2>

If that's the exact structure expected within the .INI files, personally, I'd just start with an IniReadSectionNames(). Then from that array, do a FileWriteLine() inside of a For... Next loop containing IniRead() based upon the results. There's also an XML UDF listed in the forums, but since it's already structured in an .INI file, I wouldn't make this one that complicated.

"Lightly" tested example:

CODE

$iniFile = @ScriptDir & "\convert.ini"

$xmlFile = @ScriptDir & "\convert.xml"

$writeFile = FileOpen($xmlFile,1)

$aNodes = IniReadSectionNames($iniFile)

For $i = 1 To $aNodes[0]

FileWriteLine($writeFile,"<" & $aNodes[$i] & ">")

FileWriteLine($writeFile," <gatewayID>" & IniRead($iniFile, $aNodes[$i],"gatewayID","") & "</gatewayID>")

FileWriteLine($writeFile," <name>" & IniRead($iniFile, $aNodes[$i],"name","") & "</name>")

FileWriteLine($writeFile,"</" & $aNodes[$i] & ">")

Next

FileClose($writeFile)

Edit: linked eltorro's "XML DOM wrapper" Edited by Monamo

- MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup]

Share this post


Link to post
Share on other sites

Thats exactly how I would do it. No external functions are needed unless you are reading back out or sorting in the same script.

Share this post


Link to post
Share on other sites

If that's the exact structure expected within the .INI files, personally, I'd just start with an IniReadSectionNames(). Then from that array, do a FileWriteLine() inside of a For... Next loop containing IniRead() based upon the results. There's also an XML UDF listed in the forums, but since it's already structured in an .INI file, I wouldn't make this one that complicated.

"Lightly" tested example:

CODE

$iniFile = @ScriptDir & "\convert.ini"

$xmlFile = @ScriptDir & "\convert.xml"

$writeFile = FileOpen($xmlFile,1)

$aNodes = IniReadSectionNames($iniFile)

For $i = 1 To $aNodes[0]

FileWriteLine($writeFile,"<" & $aNodes[$i] & ">")

FileWriteLine($writeFile," <gatewayID>" & IniRead($iniFile, $aNodes[$i],"gatewayID","") & "</gatewayID>")

FileWriteLine($writeFile," <name>" & IniRead($iniFile, $aNodes[$i],"name","") & "</name>")

FileWriteLine($writeFile,"</" & $aNodes[$i] & ">")

Next

FileClose($writeFile)

Edit: linked eltorro's "XML DOM wrapper"
What amazing quick response!

Thank you guys, that's realy simple code and usefull that would help me much.

Thats exactly how I would do it. No external functions are needed unless you are reading back out or sorting in the same script.

Thanks to you too

Be Green Now or Never (BGNN)!

Share this post


Link to post
Share on other sites

a bit belated to help you specifically on this problem, but in case you want to do this with ANY (valid) .ini file (not just those with only gatewayID and name in each section):

Global $iniFile=FileOpenDialog("Please select an .ini file:","","ini files (*.ini)",3)
If @error Then Exit
If NOT FileExists($iniFile) Then ExitMsg(".ini file not found!")
Global $xmlFile=FileSaveDialog("Save XML as:","","xml files (*.xml)",18)
If @error Then Exit
If NOT StringInStr(StringTrimLeft($xmlFile,StringInStr($xmlFile,"\",0,-1)),".") Then $xmlFile&=".xml"
$xmlFile=FileOpen($xmlFile,2)
If @error Then ExitMsg("There was a problem saving the XML file")

Global $iniSections=IniReadSectionNames($iniFile)
If @error Then ExitMsg("There was a problem reading the ini file's sections")
For $sectionName=1 To $iniSections[0]
    FileWriteLine($xmlFile,"<"&$iniSections[$sectionName]&">")
    Dim $sectionEntries=IniReadSection($iniFile,$iniSections[$sectionName])
    If NOT @error Then
        For $keyName=1 To $sectionEntries[0][0]
            FileWriteLine($xmlFile,@TAB&"<"&$sectionEntries[$keyName][0]&">"&$sectionEntries[$keyName][1]&"</"&$sectionEntries[$keyName][0]&">")
        Next
    EndIf
    FileWriteLine($xmlFile,"</"&$iniSections[$sectionName]&">")
Next
FileClose($xmlFile)
MsgBox(0,"Done","Conversion completed successfully")

Func ExitMsg($_ExitMsg)
    MsgBox(16,"Error",$_ExitMsg&@CRLF&@CRLF&"Exiting now.")
    Exit
EndFunc
:)

"There are 10 types of people in this world - those who can read binary, and those who can't.""We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true." ~Robert Wilensky0101101 1001010 1100001 1101101 1100101 1110011 0110011 1001101 10001110000101 0000111 0001000 0001110 0001101 0010010 1010110 0100001 1101110

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  
Followers 0