[jdom-interest] More SAXOutputter woes

Bradley S. Huffman hip at cs.okstate.edu
Wed Oct 27 14:06:06 PDT 2004


One more for the TODO list.

I was looking at SAXOutputter and notice that attribute namespaces are not
report through startPrefixMapping/endPrefixMapping.  I wrote a little test

import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import org.xml.sax.*;
import org.xml.sax.ext.*;
import org.xml.sax.helpers.*;
import java.io.*;

public class SAXTest {

   public static void main(String[] args) throws IOException {
      Namespace ns1 = Namespace.getNamespace("ns1", "http://www.acme.org/ns1");
      Namespace ns2 = Namespace.getNamespace("ns2", "http://www.acme.org/ns2");
      Namespace ns3 = Namespace.getNamespace("ns3", "http://www.acme.org/ns3");

      Element root = new Element("root", ns1);
      Element child = new Element("child", ns2);
      Attribute att = new Attribute("att", "true", ns3);

      child.setAttribute(att);
      root.addContent(child);

      try {
          SAXOutputter sax = new SAXOutputter(new TestHandler());

          sax.output(root);
      }
      catch(JDOMException ex) {
      }
   }
}

class TestHandler extends DefaultHandler {
    public void startPrefixMapping(String s1, String s2) {
        System.out.print("startPrefixMapping call ");
        System.out.println(s1 + " " + s2);
    }

    public void startElement(String namespaceURI, String localName,
                             String qName, Attributes atts)
                             throws SAXException {
        System.out.println("startElement call " + qName);
        for (int i=0, len=atts.getLength(); i<len; i++) {
            System.out.println("    Attribute " + atts.getQName(i) +
                               " " + atts.getValue(i));
        }
    }
}

And sure enough, ns3 is not reported

startPrefixMapping call ns1 http://www.acme.org/ns1
startElement call ns1:root
startPrefixMapping call ns2 http://www.acme.org/ns2
startElement call ns2:child
    Attribute ns3:att true

Brad


More information about the jdom-interest mailing list