Helma logo
helma.org » Home > development > rfc > Xml Conversion Patterns 2

Xml Conversion Patterns 2

Describes high-level conversion of XML documents to HopObject structures and back. Implemented.

Conversion between XML documents and HopObjects can be achieved in two ways:

  • convert XML
    You can read any XML-document and make Hopobjects out of it. By default Helma follows exactly the structure of the XML-document and doesn't apply any prototypes to the resulting objects. A set of rules can be applied to define which element adopts which prototype, to rename properties, to shorten the tree where needed etc. The resulting tree is the most convenient for scripting not for handling the XML document. Therefore it is detached from structure of the original document, without the possibility to write back. The rules are defined in a properties-file that is handed over to the conversion function.
        HopObject Xml.get(filename);
        HopObject Xml.get(filename,propertiesfile);
        HopObject Xml.get(url);
        HopObject Xml.get(url,propertiesfile);
    
  • dump/re-read XML
    A tree of Hopobjects is dumped to XML. Here no rules can be applied, but Helma will create an XML-document that results in exactly the same Hopobject-tree when it's re-read.
          void Xml.write(hopobject,filename);
        String Xml.writeToString(hopobject);
    
    write() stores the tree below hopobject in a file, writeToString() returns a string.
        HopObject Xml.read(filename);
        HopObject Xml.read(filename,hopobject);
        HopObject Xml.readFromString(string);
        HopObject Xml.readFromString(string,hopobject);
    
    This tries to read a XML file or string. The content of this file or string must be xml exactly the way helma generated it. If you specify a hopobject as a second parameter, new nodes will be appended to this object. So with
      Xml.write(root,filename);
      Xml.read(filename,root);
    
    the whole application cache can be dumped and read. Just be carefull that the specified object won't be reset, so if you call load several times you might have doubles in your node tree.
  • conversion properties

  • Please note that java-properties regard not only a '=' but also a ':' as the separator. So in a properties file name=test is the same as name:test. As the ':' character is used to separate namespace and localname, we need to mask these characters if they appear in the properties file.
    namespace\:localname = something
    separator = _ (default)
    The separator string used to replace the ":" between the namespace and the local name of an element.


    elementname._prototype = | story | item etc etc
    Elements of a certain name are converted hopobjects of a certain prototype.
    item._prototype = testobject
    elementname._attribute.attrname = propertyname

    A certain attribute is converted to a property of a different name.
    item._attribute.about = info
    
    <item about="xyz"></item>
    
    results in
    item.info = "xzy"
    elementname._text = propertyname
    Maps the text content to a property of a different name. By default the text content is added to the result object as property "text".
       
    item._text = info
    
    <item>some text</item>
    
    results in
    item.info = "some text"
    (the default would be item.text = "some text")

    elementname.childname._text = propertyname
    The text content of the childelement is mapped as a property of the hopobject that results from the parent element.
       
    item.description._text = desc
    
    <item>
      <description>some text</description>
      <description>more text</description>
    </item>
    
    results in
    item.desc = "some text"
    If there is more than one element of that name, the rest will be ignored.

    elementname._children = _all
    elementname._children = childelementname

    Defines which childelements are converted as children of the corresponding Hopobject. _all will mount all childelements as children (nevertheless they might get a virtual mounting too, see below). _all can lead to a children-array containing different prototypes.
    item._children = _all
    
    <item>
      <description>some text</description>
      <description>some text</description>
      <image>imagedata</image>
    </item>
    
    results in
    item.get(0) = Hopobject description
    item.get(1) = Hopobject description
    item.get(2) = Hopobject imagedata
    or
    item._children = description
    
    <item>
      <description>some text</description>
      <description>some text</description>
      <image>imagedata</image>
    </item>
    
    results in
    item.get(0) = Hopobject description
    item.get(1) = Hopobject description
    

    elementname.childrenname = newname

    By default a virtual collection of children is created for every type of element that is found in the document. With this directive you can change the name of this collection.
    item.testimage = images
    
    <item>
      <testimage>firstimage</testimage>
      <testimage>secondimage</testimage>
      <testimage>thirdimage</testimage>
    </item>
    
    results in
    item.images.get(0) = Hopobject firstimage
    item.images.get(1) = Hopobject secondimage
    item.images.get(2) = Hopobject thirdimage
    
    the default would be
    item.testimage.get(0) = Hopobject firstimage
    item.testimage.get(1) = Hopobject secondimage
    item.testimage.get(2) = Hopobject thirdimage
    


Up: Requests For Comments (RFCs)
Previous: New User Object Model Next: Cron jobs in Helma

... comment


Page last modified on 2003-05-13 13:57 by hns

 
tobi, Wednesday, 5. June 2002, 19:49
Change note
As of June 5, save(), load() and create() have been renamed to write(), read(), writeToString() and readFromString() as discussed on the mailing list http://helma.org/archives/hop/2002-June/001247.html

... link  


... comment
 
hns, Thursday, 6. June 2002, 15:26
Change note
Reversed argument order in Xml.write() from

void Xml.write(filename,hopobject);

to

void Xml.write(hopobject,filename);

because this seemed more consistent to me. This is still pending Stefan's approval.

... link  


... comment
 
stefanp, Thursday, 26. September 2002, 17:16
Re: Xml Conversion Patterns 2
be careful: If the assigned property name is the same as a predefined hopobject-function like add, remove, href, link (!) the property is not accessible.
the rss-examples have that problem when is converted to a property "link" as link() is an undocumented function of every hopobject.

... link  

 
hns, Friday, 27. September 2002, 10:11
Re: Re: Xml Conversion Patterns 2
This sucks... We have to do something about this, at some point.

BTW, I think hopObject.link() is obsolete and should be removed.

... link  


... comment