XML Parsing/Generation example -- Caravan Help


NOTE : Text in brown is caravan code.Requires Caravan 3.12 or above
We are going to parse the sample xml shown alongside
to create an instance 'xml_smaple' of an xml object
  • Parsing XML :
    object xml_sample=samplexml(file)
    No Errors : xml parsed properly
    'xml_sample' created
  • To get the TagName we can use the '_name' property :
    xml_sample(_name) : SampleXml
  • Get Number of nodes at current level using '_node' property :
    xml_sample(_node(00)) : 03
  • Get Attribute Name :
    xml_sample(_attribute(01)) : Auther
  • Get attribute Value :
    xml_sample(_attribute(Auther)) : "kdv"
    Date
    xml_sample(Date(_attribute(value))) :Tue June 8, 2004
  • Using Dot-Format to access internal node
    xml_sample(Quote.Headline) : This is the headline
  • Changing the current node to 'Quote'
    xml_sample(_node)="Quote"
  • Getting 'Headline' from this node
    xml_sample(Headline) :This is the headline
  • You cannot access the 'Headline' as before :
    xml_sample(Quote.Headline) :
  • Using the '_root' keyword to access tags above the current level
    xml_sample(_root.Quote.Headline) : This is the headline
    xml_sample(_root.Date._attribute(value)) : Tue June 8, 2004

    Reading multiple nodes having the same tag

  • Get the count of the number of such nodes
    xml_sample(body.p(00)): 04
  • Get all values of body . p
    xml_sample(body.p):
    Following is a notable quote from history. "Great Russia is rising from her knees." -- Boris Yeltsin on his inauguration as Russia's first elected president on July 10, 1991.
  • Display paragraphs one by one
    xml_sample(body.p(01)) : Following is a notable quote from history.
    xml_sample(body.p(02)) :
    xml_sample(body.p(03)) : "Great Russia is rising from her knees."
    xml_sample(body.p(04)) : -- Boris Yeltsin on his inauguration as Russia's first elected president on July 10, 1991.
  • Keyword '_next' is used to access the next node having identical tag at the same level
    xml_sample(_node)="body.p"
    loop xloop (xml_sample(_root.Quote.body.p(00)))
    xloop(count); xml_sample(_node);
    xml_sample(_node)="_next"
    repeat xloop
    01 Following is a notable quote from history.
    02
    03 "Great Russia is rising from her knees."
    04 -- Boris Yeltsin on his inauguration as Russia's first elected president on July 10, 1991.
  • Keyword '_previous' is used to access the higher level node
    xml_sample(_previous._previous.Headline) :
  • When a node is output the xml of the node is generated
    xml_sample(_root.Quote.body) :

    Following is a notable quote from history.

    "Great Russia is rising from her knees."

    -- Boris Yeltsin on his inauguration as Russia's first elected president on July 10, 1991.

  • The above is equivalent to generating the XML of current node by the syntax 'objectname()'. The xml on the right is generated by the statement xml_sample(_node)="_root";xml_sample()

  • ADDING a new node to the xml
    xml_sample(_node)="_root.Quote"; // go to the required node
    xml_sample(MyNewNode)="";//To make a node, assign empty string using the required tag
    xml_sample(_node)="MyNewNode";// go to this node
    xml_sample(_attribute.PS)="This node was added by "caravan""; // adding attribute 'PS'
    xml_sample(MyComment)="No Comment";// add a new property here
    You can see the new node in the XML generated at right
    This was added to the existing xml

  • One can also declare an empty xml object as:
    object xyz

    This is used to generate XML from scratch
    Start by assigning the root tag:
    xyz(MyNEWXML)=""

    Finally go to the root and generate the xml by:
    xyz(_node)="_root";xyz()

  • Sample XML:
    Generate XML output : xml_sample();
    <SampleXml Auther=""kdv"" node="root">
    <Date value="Tue June 8, 2004"/>
    <Quote>
    <Headline>This is the headline</Headline>
    <body>
    <p> Following is a notable quote from history.</p>
    <p> </p>
    <p> "Great Russia is rising from her knees."</p>
    <p> -- Boris Yeltsin on his inauguration as Russia's first elected president on July 10, 1991. </p>
    </body>
    <MyNewNode PS="This node was added by "caravan"">
    <MyComment>No Comment</MyComment>
    </MyNewNode>
    </Quote>
    <credit>Reuters News Agency</credit>
    </SampleXml>