DOM Example

private Document plist;

protected void setUp() 
  throws IOException, ParserConfigurationException, SAXException {
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  factory.setNamespaceAware(true); // NEVER FORGET THIS!
  DocumentBuilder builder = factory.newDocumentBuilder();
  
  plist = builder.parse(new File("thunderbirdplist.xml"));
}

public void testNoTwoKeyElementsAreAdjacentDOM() {
   
  Element root = plist.getDocumentElement();
  Element dict = (Element) root.getElementsByTagName("dict").item(0);
  NodeList children = dict.getElementsByTagName("*");
  for (int i = 0; i < children.getLength(); i++) {
    Node element = children.item(i);
    if (element.getNodeName().equals("key")) {
      assertFalse(children.item(i+1).getNodeName().equals("key"));
      // effectively also tests that every key 
      // is followed by something
    }
  }
  
}

Previous | Next | Top | Cafe con Leche

Copyright 2005-2007 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified September 23, 2005