Daniel Kirsch wrote:
Hi,
I have a XML document which I want to show as a tree in my XUL application. The tree will have several sublevels and should be changeable by JavaScript.


I looked at how the DOM Inspector create and handle it's tree however I don't understand how it works.

Is there any simple example which just creates a XUL tree from an XML document?
Or may someone tell how the DOM Inspector creates an instance of the "inTreeBuilder" class which, according to "inspector/viewers/dom.xul", will create the tree content? I couln't found anything which makes sense to me.


Thanks
Daniel


I make not gurarantees that this code will do what you need. AAMOF, I am sure the top level node needs to be fixed. The rest of the code seems to work fairly well for me. It's obviously Java, not JavaScript, but you should be able to port it without much trouble. I haven't attemted to clean up the imports, so there's probably a bunch of junk in them.


package grammar.ui;

import org.w3c.dom.Element;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.ServletException;
import java.net.URI;
import java.net.URISyntaxException;
import org.xml.sax.SAXException;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.PrintWriter;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import java.io.File;
import org.w3c.dom.NamedNodeMap;


public class TreeViewer { public static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

public static final String CHROME_LINK =
"\n<?xml-stylesheet href=\"chrome://global/skin/\" type=\"text/css\"?>\n";


  public TreeViewer() {
  }


public void createXul(Document xmlSourceDocument, Document canvasDocument)
throws ServletException {
Element window = canvasDocument.createElement("window");
window.setAttribute("xmlns:html","http://www.w3.org/1999/xhtml";);


window.setAttribute("xmlns","http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";);
canvasDocument.appendChild(window);
this.buildResultTree(canvasDocument.getDocumentElement(), xmlSourceDocument.getDocumentElement());
}


  public void createXul(String xmlSourceName, OutputStream out)
    throws ServletException {

    URI uri = null;
    try {
      uri = new URI(xmlSourceName);
    }
    catch (URISyntaxException ex) {
      throw new ServletException(ex);
    }

    Document xmlSourceDocument = null;
    try {
      xmlSourceDocument = DocumentBuilderFactory
                        .newInstance()
                        .newDocumentBuilder()
                        .parse(new File(uri));
    }
    catch (SAXException ex) {
      throw new ServletException(ex);
    }catch (IOException ex) {
      throw new ServletException(ex);
    }catch (ParserConfigurationException ex) {
      throw new ServletException(ex);
    }

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);
    dbf.setNamespaceAware(true);
    DocumentBuilder db = null;
    try {
      db = dbf.newDocumentBuilder();
    }
    catch (ParserConfigurationException ex) {
      throw new ServletException(ex);
    }
    Document canvasDocument = db.newDocument();

this.createXul(xmlSourceDocument, canvasDocument);

    try {
      this.seralize(canvasDocument,out);
    }
    catch (IOException ex) {
      throw new ServletException(ex);
    }
  }

  public void buildResultTree(Element canvas, Element rootElement){
    Element tree;
    Element children;
    Element item;
    Element row;
    Element cell;
    Element columns;
    Element column;
    ArrayList childElements;
    Document document;

document = canvas.getOwnerDocument();

    tree = (Element)canvas.appendChild(document.createElement("tree"));
    tree.setAttribute("flex","1");

columns = (Element)tree.appendChild(document.createElement("treecols"));

column = (Element)columns.appendChild(document.createElement("treecol"));
column.setAttribute("id","rootelement");
column.setAttribute("label","DOM Tree View");
column.setAttribute("primary","true");
column.setAttribute("flex","1");


children = (Element)tree.appendChild(document.createElement("treechildren"));

item = (Element)children.appendChild(document.createElement("treeitem"));

    row = (Element)item.appendChild(document.createElement("treerow"));
    cell = (Element)row.appendChild(document.createElement("treecell"));
    cell.setAttribute("label", rootElement.getTagName());

childElements = getChildElements(rootElement);

if(!childElements.isEmpty()) {
item = (Element)children.appendChild(document.createElement("treeitem"));
item.setAttribute("container","true");
item.setAttribute("open","true");
children = (Element)item.appendChild(document.createElement("treechildren"));
Iterator childIterator = childElements.iterator();
while(childIterator.hasNext()) {
makeBranch(children, (Element)childIterator.next());
}
}
}
private void makeBranch(Element parent, Element element){
Element children;
Element item;
Document document = parent.getOwnerDocument();
item = (Element)parent.appendChild(document.createElement("treeitem"));
this.makeElementRow(item, element);


NamedNodeMap attributeNodes = element.getAttributes();

if(element.hasAttributes()) {

item = (Element)parent.appendChild(document.createElement("treeitem"));
item.setAttribute("container","true");
item.setAttribute("open","true");


children = (Element)item.appendChild(document.createElement("treechildren"));
for(int i = 0; i <attributeNodes.getLength(); i++){
this.makeAttributeRow(children, attributeNodes.item(i));
}
}


ArrayList childElements = getChildElements(element);

    if(!childElements.isEmpty()){
      item.setAttribute("container","true");
      item.setAttribute("open","true");

children = (Element)item.appendChild(document.createElement("treechildren"));

Iterator childIterator = childElements.iterator();

      while(childIterator.hasNext()) {
        this.makeBranch(children, (Element)childIterator.next());
      }
    }
  }

private void makeElementRow(Element parentItem, Element element){
Element row;
Element cell;
Document document = parentItem.getOwnerDocument();
row = (Element)parentItem.appendChild(document.createElement("treerow"));
cell = (Element)row.appendChild(document.createElement("treecell"));
cell.setAttribute("label",element.getTagName());


}

  private void makeAttributeRow(Element parent, Node attribute){
    Element row;
    Element cell;
    Document document = parent.getOwnerDocument();
    row = (Element)parent.appendChild(document.createElement("treerow"));

    cell = (Element)row.appendChild(document.createElement("treecell"));
    cell.setAttribute("label",attribute.getNodeName());

    cell = (Element)row.appendChild(document.createElement("treecell"));
    cell.setAttribute("label",attribute.getNodeValue());

}

  private ArrayList getChildElements(Element element) {
    ArrayList childElements = new ArrayList();
    NodeList nodes = element.getChildNodes();
    for(int i = 0; i < nodes.getLength(); i++){
      if(nodes.item(i).getNodeType() == nodes.item(i).ELEMENT_NODE){
        childElements.add(nodes.item(i));
      }
    }
    return childElements;
  }

  private void seralize(Document document, OutputStream out)
      throws IOException {
    OutputFormat of = new OutputFormat();
    of.setIndenting(true);
    of.setIndent(1);
    of.setLineWidth(0);
    //I had to do this so I could include the linked style
    of.setOmitXMLDeclaration(true);
    this.XML_HEADER.getBytes();
    out.write(this.XML_HEADER.getBytes());
    out.write(this.CHROME_LINK.getBytes());
    XMLSerializer srzr = new XMLSerializer(of);
    srzr.setOutputByteStream(out);
    srzr.serialize(document);
  }

  public static void main(String[] args) {
    TreeViewer treeViewer1 = new TreeViewer();

String xmlSourceName = "file:///home/myuid/jbproject/grammar/data/norse-verb.xml";


TreeViewer tv = new TreeViewer();


    try {
      tv.createXul(xmlSourceName,System.out);
    }
    catch (ServletException ex) {
      ex.printStackTrace();
    }
  }
}




Reply via email to