[jdom-interest] newbie help on parsing xml with namespaces

Elliotte Rusty Harold elharo at metalab.unc.edu
Fri Jul 26 17:21:03 PDT 2002


At 1:24 PM -0700 7/26/02, Charlie Wu wrote:

>I'm having some trouble xml with namespaces.. my xml file is like the
>following:
>
><?xml version='1.0' encoding='UTF-8' ?>
><AuthLoginResponse version="1.0" xml:lang="en" status="0"
>xmlns="com:abc:developer">
>	<Control>
>		<SessionID>12345/SessionID>
>	</Control>
></AuthLoginresponse>
>
>I want to be able to retrieve the SessionID.. I'm adapting the
>WarReader.java to do this.. but I noticed that
>if I don't indicate the namespace I'm not getting anything.. but how do I
>set the namespace? I thought
>"com:abc:developer" should be the prefix but compiler is saying prefix can
>not have colons..

Another wonderful example of a developer being confused by namespaces 
and therefore causing JDOM to fail. You can't use JDOM properly 
unless you use namespaces properly (or not at all).

This document is very weird. It puts all elements in the default 
namespace which is com:abc:developer. No element has a prefix. 
Furthermore, the namespace name really isn't a URI here like it's 
supposed to be. Since you haven't provided complete code, I can't be 
sure of the problem; but it's probably that you asked for a namespace 
with a prefix when the namespace you're using has no namespace. You 
really need to fix your XML document first, before you worry about 
writing JDOM code to process it. Something like this:

<abc:AuthLoginResponse version="1.0" xml:lang="en" status="0"
xmlns:abc="http://www.example.com/MyNamespace">
	<abc:Control>
		<abc:SessionID>12345/abc: SessionID>
	</abc: Control>
</abc:AuthLoginresponse>

This isn't your fault. Namespaces are a confusing mess. JDOM 
faithfully adheres to that confusing mess.
-- 

+-----------------------+------------------------+-------------------+
| Elliotte Rusty Harold | elharo at metalab.unc.edu | Writer/Programmer |
+-----------------------+------------------------+-------------------+
|          XML in a  Nutshell, 2nd Edition (O'Reilly, 2002)          |
|              http://www.cafeconleche.org/books/xian2/              |
|  http://www.amazon.com/exec/obidos/ISBN%3D0596002920/cafeaulaitA/  |
+----------------------------------+---------------------------------+
|  Read Cafe au Lait for Java News:  http://www.cafeaulait.org/      |
|  Read Cafe con Leche for XML News: http://www.cafeconleche.org/    |
+----------------------------------+---------------------------------+



More information about the jdom-interest mailing list