sponsor Vim development Vim logo Vim Book Ad

basicXmlParser : Create object trees and save them as xml, or create trees from xml files.

 script karma  Rating 0/0, Downloaded by 1325  Comments, bugs, improvements  Vim wiki

created by
Alexandre Viau
 
script type
utility
 
description
See latest version on github: https://github.com/viaa/VimPlugins

Documentation

Name: basicXmlParser.vim
Version: 1.1
Description: Plugin create object trees and get them or save them as xml, and to create object trees from a xml files. May be used as an object tree, the xml is the file format for load and save the object tree from and to a file. Serialization to xml is optional, the user is not required to load or to save to xml, the tree may be builded manually or programmatically. This plugin was created as a utility plugin to load and save configuration files. See the usage section on how to use the plugin.
Author: Alexandre Viau (alexandreviau@gmail.com)
Installation: Copy the plugin to the vim plugin directory.

Usage:

The following examples create, save to file and load from file the following xml tree:

<root>
   <Marks>
      <A>
         C:\Usb\i_green\apps
         <myLevel3>
            myLevel3Value
            <myLevel4>
               myLevel4Value
            </myLevel4>
         </myLevel3>
      </A>
      <B>
         C:\Users\User\Desktop\
      </B>
      <C>
         C:\
      </C>
   </Marks>
   <LastPath>
      d:\
   </LastPath>
</root>

<item name="root">
   <item name="Marks">
      <item name="A">
         C:\Usb\i_green\apps
         <item name="myLevel3">
            myLevel3Value
            <item name="myLevel4">
               myLevel4Value
            </item>
         </item>
      </item>
      <item name="B">
         C:\Users\User\Desktop
      </item>
      <item name="C">
         C:\
      </item>
   </item>
   <item name="LastPath">
      d:\
   </item>
</item>


Create object trees and get them or save them as xml

NOTE: Since the objects are really dictionaries, vim offers 2 syntaxes to interact with their content. One is like this for example "myRoot.LastPath.Value" the other is "myRoot['LastPath'].Value", in the following examples the first syntax is used but the second would work too.

Root level
let myRoot = g:Item.New()

First level
cal myRoot.Add(g:Item.New2('LastPath', 'd:'))
echo myRoot.LastPath.Value

cal myRoot.Add(g:Item.New1('Marks'))
echo myRoot.Marks.Value

Second level
cal myRoot.Marks.Add(g:Item.New2('A', 'C:\Usb\i_green\apps'))
cal myRoot.Marks.Add(g:Item.New2('B', 'C:\Users\User\Desktop'))
cal myRoot.Marks.Add(g:Item.New2('C', 'C:'))
echo myRoot.Marks.A.Value
echo myRoot.Marks.B.Value
echo myRoot.Marks.C.Value
Show how many items there is in the second level
echo myRoot.Marks.Count()

Third level
cal myRoot.Marks.A.Add(g:Item.New2('myLevel3', 'myLevel3Value'))
echo myRoot.Marks.A.myLevel3.Value
Show how many items there is in the third level
echo myRoot.Marks.A.Count()

Fourth level
cal myRoot.Marks.A.myLevel3.Add(g:Item.New2('myLevel4', 'myLevel4Value'))
echo myRoot.Marks.A.myLevel3.myLevel4.Value

To get the xml as a list
let myXmlList = myRoot.ToXmlList()
echo myXmlList

To save the xml to a file
cal myRoot.SaveToXml('c:/temp/cfg.xml')

Remove an item
cal myRoot.Marks.Remove('A')
echo myRoot.Marks.A.Value " Should give an error saying that the key cannot be found

Check for existance of an item
if myRoot.Marks.Contains('A') == 1
  echo "Contains the item A"
else
  echo "Dosen't contain the item A"
endif

Example to copy from third level to a new root. The "Clone()" method does a complete copy of the current item, without cloning, the affected variable becomes a refrence and modifying it would modify the original item as well. So use the "Clone()" methods if an independent copy is needed.
let myRoot2 = myRoot.Marks.A.myLevel3.Clone()
Display value from new root, now what was 4th level is now first level
echo myRoot2.myLevel4.Value

Create object trees from a xml files

NOTE: Since the objects are really dictionaries, vim offers 2 syntaxes to interact with their content. One is like this for example "myRoot.LastPath.Value" the other is "myRoot['LastPath'].Value", in the following examples the first syntax is used but the second would work too.

Load the file created in the previous example
let myRoot2 = g:Item.LoadFile('c:/temp/cfg.xml')

Show some elements from the loaded xml file
echo myRoot2.LastPath.Value
echo myRoot2.Marks.A.Value
echo myRoot2.Marks.B.Value
echo myRoot2.Marks.A.myLevel3.Value
echo myRoot2.Marks.A.myLevel3.myLevel4.Value
echo myRoot2.Marks.C.Value
echo myRoot2.Marks.Value

Create object trees from expressions

There is another way of creating the object trees using expressions, the "g:RootItem.LoadFile()" function is an example how to do this. By expressions, I mean that something like exe 'cal items.Items' . level . '.Add(g:Item.New("' . tag . '", ""))' may be used where the "level" variable would contain a string of the path to the items like for example "myItem1.Items.myItem2.Items".
Todo:
1. Automatically put every tag and values each on his own line (it should be like this to be parsed correctly)
2. Get multiline values inside tags
3. Add some kind of iterator (like in the configuration utility with an ienumerator syntax) to loop the items (without having to check in the client code that the value is a dictionary) See ShowMarks function in vimExplorer where a check for dictionary type is needed and checking if dictionary empty and also a key, value foreach loop dosen't work, the value is not set and there is need to get it using the full g:VeCfg.Mark[key].Value
   Or at least find a way to filter out the items that are not dictionaries using the filter command.
   Give examples how to iterate items in the usage section    
4. Maybe find a way that to get or set a value that does not exist a check and creation would not be needed in the client code (maybe through a function)


History:

1.0 Initial release
1.1 Added a function to remove items from the tree "g:Item.Remove(name)" by specifying its name in parameter
    Added the "Contains(name)" function to check existance of an item
    Changed the file format to display the xml tree with each tag being an "item" tag having a name attribute instead of having the name as the tag itself. One avantage of this format is to being able to have a "/" in the tag name. So a xml tree formely in a format like this:
<root>
   <Marks>
      <A>
         C:\Usb\i_green\apps
         <myLevel3>
            myLevel3Value
            <myLevel4>
               myLevel4Value
            </myLevel4>
         </myLevel3>
      </A>
      <B>
         C:\Users\User\Desktop\
      </B>
      <C>
         C:\
      </C>
   </Marks>
   <LastPath>
      d:\
   </LastPath>
</root>

is now in a format like this:

<item name="root">
   <item name="Marks">
      <item name="A">
         C:\Usb\i_green\apps
         <item name="myLevel3">
            myLevel3Value
            <item name="myLevel4">
               myLevel4Value
            </item>
         </item>
      </item>
      <item name="B">
         C:\Users\User\Desktop
      </item>
      <item name="C">
         C:\
      </item>
   </item>
   <item name="LastPath">
      d:\
   </item>
</item>
 
install details
Installation: Copy the plugin to the vim plugin directory.
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
basicXmlParser.vim 1.1 2013-01-19 7.0 Alexandre Viau Added a function to remove items from the tree "g:Item.Remove(name)" by specifying its name in parameter
Added the "Contains(name)" function to check existance of an item
Changed the file format to display the xml tree with each tag being an "item" tag having a name attribute instead of having the name as the tag itself. One avantage of this format is to being able to have a "/" in the tag name. So a xml tree formely in a format like this:
<root>
   <Marks>
      <A>
         C:\Usb\i_green\apps
         <myLevel3>
            myLevel3Value
            <myLevel4>
               myLevel4Value
            </myLevel4>
         </myLevel3>
      </A>
      <B>
         C:\Users\User\Desktop\
      </B>
      <C>
         C:\
      </C>
   </Marks>
   <LastPath>
      d:\
   </LastPath>
</root>

is now in a format like this:

<item name="root">
   <item name="Marks">
      <item name="A">
         C:\Usb\i_green\apps
         <item name="myLevel3">
            myLevel3Value
            <item name="myLevel4">
               myLevel4Value
            </item>
         </item>
      </item>
      <item name="B">
         C:\Users\User\Desktop
      </item>
      <item name="C">
         C:\
      </item>
   </item>
   <item name="LastPath">
      d:\
   </item>
</item>
basicXmlParser.vim 1.0 2013-01-02 7.0 Alexandre Viau Initial upload
ip used for rating: 3.141.199.243

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github