[jdom-interest] How to read xml files with namespaces

Michael Kay mike at saxonica.com
Thu Aug 6 09:24:46 PDT 2009


The name of the element is not "drawing", it is
{http://www.intellidata.net/resources}drawing.
 
So you want rootElem.getChildren("drawing",
"http://www.intellidata.net/resources");
 
Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay 

 

 
 


  _____  

From: jdom-interest-bounces at jdom.org [mailto:jdom-interest-bounces at jdom.org]
On Behalf Of Brian Trezise
Sent: 06 August 2009 17:09
To: jdom-interest at jdom.org
Subject: [jdom-interest] How to read xml files with namespaces



Ok, so I'm having trouble reading an XML file with a namespace.  I've been
hunting and searching for a solution for several hours now, and I can't
figure out what I'm doing wrong.

 

Heres a sample of the xml:

 

<?xml version="1.0"?>

<drawings 

xmlns="http://www.intellidata.net/resources"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.intellidata.net/resources
IntelliSpecDrawings.xsd">

            <drawing>

                        <drawing-name>AxAlum0</drawing-name>

                        <family-prefix>CAA</family-prefix>

                        <dimension>

                                    <dimension-name>Lead Length
(Neg)</dimension-name>

                                    <dimension-symbol>LL-</dimension-symbol>

                        </dimension>

                        <dimension>

                                    <dimension-name>Length</dimension-name>

                                    <dimension-symbol>L</dimension-symbol>

                        </dimension>

                        <dimension>

                                    <dimension-name>Lead Length
(Pos)</dimension-name>

                                    <dimension-symbol>LL+</dimension-symbol>

                        </dimension>

                        <dimension>

                                    <dimension-name>Lead
Diameter</dimension-name>

                                    <dimension-symbol>F</dimension-symbol>

                        </dimension>

                        <dimension>

 
<dimension-name>Diameter</dimension-name>

                                    <dimension-symbol>D</dimension-symbol>

                        </dimension>

            </drawing>

</drawings>

 

There can be many <drawing> tags with the above contents, but there are no
other objects permitted as direct children of <drawings>.

 

I'm using the following code to read the xml file:

 

      File drawingXmlFile = new File(config.getDrawingXmlFile());

      SAXBuilder saxBuilder = new SAXBuilder();

      Document doc = saxBuilder.build(new BufferedInputStream(new
FileInputStream(drawingXmlFile)));

 

      Element rootElem = doc.getRootElement();

      List<Element> drawingElems = rootElem.getChildren("drawing");

 

drawingElems consistently comes back with size 0.  I have tried several
variations, including simply "rootElem.getChildren()" with no success.  I
know that the problem relates to namespaces in some way, because if I take
out the namespace from the root element of the xml, it just works.  However,
this solution is not ideal as this document is intended to be maintained by
somebody without a lot of experience in XML, so the validation provided by
using the namespace would be very helpful.

 

Can anybody give me any pointers as to what I'm doing wrong?

Thanks in advance,

 

-----------------------------

Brian Trezise

IntelliData, Inc.

Staff Software Engineer

brian.trezise at intellidata.net

t: 720.524.4864

m: 303.809.9782

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.jdom.org/pipermail/jdom-interest/attachments/20090806/6959d3ff/attachment.htm


More information about the jdom-interest mailing list