Tools Update: RSSApplet and Xparse-J (4/4) - exploring XML | WebReference

Tools Update: RSSApplet and Xparse-J (4/4) - exploring XML

Tools Update: RSSApplet and Xparse-J

  ...
  public Node parse(String src) {
    count = 0;
    index = new JSArray();
  	Frag frag = new Frag();
	// remove bad \r characters and the prolog
  	frag.str = prolog(src);
	// create a root element to contain the document
  	Node root = Node.createRootelement();
	root.name="ROOT";
  	// main recursive function to process the xml
	frag = compile(frag);
  	// all done, lets return the root element + index + document
	root.contents = frag.ary;
  	root.index = index;
  	index = new JSArray();
  	return root;
  }
  ...
}  

The main user function is parse which turns a string containing the XML data into a tree of Nodes. Internally it uses the compile method recursively to process the string and build up the tree, passing around instances of the inner class Frag ment. The recursive design and the flexibility of JSArray result in a very compact implementation of just slightly over six kilobytes!

Xparse.parse() returns a Node that is the root node of the XML document, under which a tree of nodes represents the rest of the parsed document. The key user functions in Node are:

  /**
   * returns the character data in the first child element;
   * returns nonsense if the first child element ist not chardata
   *
   * @return    the characters following an element
   */
  public String getCharacters() {
    return ((Node)contents.elementAt(0)).value;
  }
  /**
   * find the node matching a certain occurance of the path description
   *
   * @param     path an XPath style expression without leading slash
   * @param     occur the n'th occurance of a node matching the path expression
   *
   * @return    the n'th Node matching the path description
   */
  public Node find(String path, int occur) {
    Node n = this;
    JSArray a = new JSArray();
    a.split(path, "/");
    int i = 0;
    while (i 

So node.getCharacters() would return "itemtext" from something like <item>itemtext</item>. node.find("rss/channel/title", 1) will return the first Node matching the given simple XPath expression.

Last not least JSArray mimics a JavaScript array for the purposes of Xparse, it is not a full-fledged generic implementation. It is not externally visible from Xparse but builds up the tree structure between Nodes. It contains the usual getter and setter functions:

  /**
   * gets the object with a certain index
   */
  Object elementAt(int idx) {
    return v.elementAt(idx);
  }
  /**
   * sets the object with a certain index
   */
  void setElementAt(Object val, int idx) {
    v.insertElementAt(val, idx);
  }
  /**
   * sets the property of an object with a certain index
   */
  void setElementAt(Object val, int idx, String prop) {
    Node n = (Node) v.elementAt(idx);
    if (prop == Name) {
      n.name = (String) val;
    }
    else if (prop == Attributes) {
      n.attributes = (Hashtable) val;
    }
    else if (prop == Contents) {
      n.contents = (JSArray) val;
    }
    else if (prop == Value) {
      n.value = (String) val;
    }
  }
  /**
   * returns the length / size of the array
   */
  int length() {
    return v.size();
  }

JSArray also has conversion functions to and from strings. Perl hackers recognize the well-known split() and join() functions for splitting a string into array elements and vice versa joining array elements into one string:

  /**
   * splits a string into an array of strings, broken at a distinct separator
   *
   * @param     str the String to be split
   * @param     sep the seperator at which to split
   */
  void split(String str, String sep) {
    v.removeAllElements();
    int oldidx = 0, newidx, skip = sep.length();
    while((newidx = str.indexOf(sep, oldidx)) != -1) {
      v.addElement(str.substring(oldidx, newidx));
      oldidx = newidx + skip;
    }
    v.addElement(str.substring(oldidx));
  }
  /**
   * join this array into one string delimited by a separator
   *
   * @param     sep the seperator to put in between the array elements
   * @return    the joined String
   */
  String join(String sep) {
    int no = 0;
    StringBuffer sb = new StringBuffer();
    while (no 
 

Xparse is also available for download in the XML tools section of this column.

http://www.internet.com

Produced by Michael Claßen
All Rights Reserved. Legal Notices.

URL: http://www.webreference.com/xml/column26/4.html
Created: Dec 15, 2000
Revised: Dec 15, 2000