users@jaxb.java.net

Re: marshal to dom

From: zhidong zhao <zzhao_at_CS.UNO.EDU>
Date: Tue, 28 Jan 2003 09:29:49 -0700

I did that test long ago and forgot some details. I tried it again this morning. Neither with ns nor without ns work. The test code is listed below.
the xpath method should be selectNodeList or selectSingleNode from org.apache.xpath.XPathAPI. It operates on w3c node.

  public static void main(String[] args) throws Exception
  {
    Unmarshaller um = new MicromagSchema.ObjectFactory().createUnmarshaller();
    Marshaller m = new MicromagSchema.ObjectFactory().createMarshaller();
    Object o = um.unmarshal(new URL("http://jacob.chem.uno.edu/schemas/newSample2.xml"));
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = db.newDocument();
    org.w3c.dom.Element root = doc.createElement("Root");
    m.marshal(o, root);
    root = (org.w3c.dom.Element)root.getFirstChild();
    String uri = root.getNamespaceURI(); //correctly returns my schema location
    String name = root.getTagName(); //returns ns1:Task where Task is name of my root element
    NodeList nl = root.getElementsByTagName(name); //getLength() returns zero
    for (int i=0; i<nl.getLength(); i++) System.out.println(nl.item(i));
    nl = root.getElementsByTagNameNS(name, uri); //nl is null from this call
    for (int i=0; i<nl.getLength(); i++) System.out.println(nl.item(i));
  }