[jdom-interest] Re: Not finding attributes on an element

Scott Ellsworth scott at alodar.com
Mon Jul 30 19:49:40 PDT 2001


It appears that the lookup for the namespace is failing in 
SAXHander.startElement.  There may be something ill formed, so I created a 
small XML file and java source file that produce the error.

Code and xml are attached, but if you look at this block in the above 
function in SAXHandler.java:

if (attLocalName != attQName) {
     String attPrefix = attQName.substring(0, attQName.indexOf(":"));
       System.out.println("SXBuilding attPrefix "+attPrefix);
       System.out.println("ns: "+getNamespace(attPrefix));
     attribute = factory.attribute(attLocalName, atts.getValue(i),
                  getNamespace(attPrefix));
   } else {
     attribute = factory.attribute(attLocalName, atts.getValue(i));
   }
   element.setAttribute(attribute);

I see the following in my log file:

SAXHandler.startElement("http://iceweasel.com/xml/RQbyScott", "skill", 
"RQbyScott:skill" , org.xml.sax.helpers.AttributesImpl at 60420f)

The attributes logged by
cat.error("attr "+i+": "+attLocalName+" "+attQName);
are
  attr 0: is_rune_skill RQbyScott:is_rune_skill

So we see that the attribute Qname has the namespace.

cat.error("attPrefix "+attPrefix);
produced
attPrefix RQbyScott

so the namespace was grabbed.

When looked up with
cat.error("ns: "+getNamespace(attPrefix));

I saw:
[Namespace: prefix "" is mapped to URI ""]

Thus, for some reason, it is not being found.

For the curious: read the following:

<?xml version="1.0" encoding="UTF-8"?>
<RQbyScott:character xmlns:RQbyScott="http://iceweasel.com/xml/RQbyScott">
	<RQbyScott:skill RQbyScott:is_rune_skill="true">
	<RQbyScott:skill_name>Dodge</RQbyScott:skill_name>
	</RQbyScott:skill>
</RQbyScott:character>

with

package com.standingstones.gaming.rqCharacter;
import java.io.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.apache.log4j.Category;
import org.apache.log4j.PropertyConfigurator;
public class RQCharacter{
     static Category cat = Category.getInstance(RQCharacter.class.getName());
     static {
         PropertyConfigurator.configure("RQCharacter.lcf");
     }
	public static void main(String[] args){
		Namespace ns=Namespace.getNamespace("RQbyScott", 
"http://iceweasel.com/xml/RQbyScott");
		BufferedInputStream instream=null;
		try{
			SAXBuilder builder=new SAXBuilder(false);
			instream=new BufferedInputStream(new FileInputStream(new 
File("test.xml")), 10000);
			try{
				Document doc=builder.build(instream);
				Element elem=doc.getRootElement();
				cat.info("Root: "+elem);
				Element skill=elem.getChild("skill", ns);
				String name=skill.getChildText("skill_name", ns);
				cat.debug("Reading "+name);
				List attributes=skill.getAttributes();
				String uri = ns.getURI();
				cat.debug("ns "+ns);
				Iterator i = attributes.iterator();
				while (i.hasNext()) {
					Attribute att = (Attribute)i.next();
					cat.debug("Att "+att+" has namespace URI "+att.getNamespaceURI());
				}
			}
			finally{ if (instream!=null) instream.close(); }
		}
		catch(IOException e) { cat.error("Error reading test.xml",e); }
		catch(JDOMException e){ cat.error("Error parsing test.xml",e); }
	}
}

Scott
Scott Ellsworth
scott at alodar.com




More information about the jdom-interest mailing list