Jump to content

Recommended Posts

  • Moderators

Modern browsers have an enormous number of built-in functions, sometimes very handy

These ones, having to do with tree handling, came up the other day : domParsing, nodeIteration and treeWalking

The built in domParser function takes a tree in html/dom or xml format :

<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer''s Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications
      with XML.</description>
   </book>
</catalog>  

which can be used as in
 

var oDOM: variant := new JObject;

asm
var oParser = new DOMParser();
@oDOM = oParser.parseFromString(@theaboveXMLstring, "application/xml");
end;

writeln(oDOM.documentElement.nodeName);                     // root (catalog)
writeln(oDOM.children[0].children[0].attributes[0].name);   // id
writeln(oDOM.children[0].children[0].attributes[0].value);  // bk101
writeln(oDOM.children.item(0).innerHTML);

 

To traverse these types of trees, there are also the following functions available : nodeIterator and treeWalker

These functions are largely similar. The nodeIterator :
 

var nodeIterator : variant := new JObject;
nodeIterator := oDOM.createNodeIterator(
    oDOM.documentElement,-1);        // NodeFilter.SHOW_ELEMENT : -1 or 0xFFFFFFFF

var currentNode := nodeIterator.nextNode();
while currentNode <> null do begin      // null = eof
  // get rid of whitespace text nodes
  if currentNode.nodeName <> '#text' then begin
    writeln(currentNode.nodeName);
    writeln(currentNode.innerHTML);
  end;
  currentNode := nodeIterator.nextNode();
end;


 

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...