[JAVA] generic approach XML <-> Hashtable

Discussions

XML & Web services: [JAVA] generic approach XML <-> Hashtable

  1. [JAVA] generic approach XML <-> Hashtable (3 messages)

    I have several large web forms need to convert to xml
    and xml need to convert back to web form display
    and all the xml(s) use differenct schema
    as u know all the web form send request
    to server is in name-value pair(just like a hashtable)
    everything get from java is just request.get......
    how can I convert it to xml?
    the name and value pair only describe 2 things. Name and value !
    but xml has structure,
    each elements has it own parent-child relationship
    my xml is NOT as below
    <book>
       <author>xxxxx</author>
       <date>xxx</date>
         :
       <address>xxx</address>
    </book>

    my xml is like below
    <book>
       <author>xxxx</author>
       <author>yyyy</author>
       <page>
           <num>1</num>
           <content>xxxxxxx</content>
           <content>yyyyyyy</content>
       </page>
       <page>
           <num>2</num>
           <content>dsdsxx</content>
           <content>sdsdy</content>
       </page>
    </book>

    how can I represent that kind of
    complex structure in name-value structure(Hashtable)
    moveover, the element will have attribute and namespace

    is there any generic approach to achieve this?
    I can success use SAX change the above to below
    key: /book
    key: /book/author_1 data: xxxx
    key: /book/author_2 data: yyyy
    key: /book/page_1/num date: 1
           : :
           : :

    it seem that I use a customise design XPath for the key
    such that I can know the parent-child relationship cater the same element name
    but after I make that, my work seem to develop a parser!!!

    anyone have idea to exhange xml <-> web form parameter value (hashtable)?
    I need write a class for each xml is a must??????
  2. I recently had a similar requirement for providing various XML files in a Hashtable format, which was going to be converted back into XML later on in the process.

    Our approach was to use the same XPath-like syntax as you mentioned.

    For example,

    <root>
      <sub>
        <field attr="attrValue">
            <attr>nestedAttrValue</attr>
        </field>
        <field>nestedValue</field>
      </sub>
      <sub>
        <field>anotherValue</field>
      </sub>
    </root>

    equals:

    "/root/sub_1/field_1/attr_1" = "attrValue"
    "/root/sub_1/field_1/attr_2" = "nestedAttrValue"
    "/root/sub_1/field_2" = "nestedValue"
    "/root/sub_2/field" = "anotherValue"

    In other words, we suffixed multiple elements with "_[counter]". Single elements were left as is without "_1". Attributes could've been marked as "/path/to/@attr" etc. but we didn't consider that necessary for our purposes.

    It worked, and although the XML files from which the Hashtable was created varied a lot, only one XmlToHashtableHandler needed to be coded.
  3. Thanks for your ans
    And now I start to consider the performance of this approach. As you know it need to parse the "XPath-like" name string by myself, I worry about the performance to parse this.

    Even I guess the performance of XML -> Hashtable is acceptable. I concern about the performance of Hashtable -> XML. do u you any idea about this?

    In fact, my real task is to convert a web form to xml and convert xml back to a web form for display. I think the name of input field is so ugly when it is "/root/sub_1/sub2_1/field_3" >.<
  4. The performance was not a problem for me. My SAX handler implementation used a java.util.Stack to keep track on "where in the XML structure we are right now".

    The code didn't look good, though, but that's just because "XML-to-Hashtable-to-XML" is somewhat awkward...