How to read xml files with namespaces

"Brian Trezise" <[email protected]>
Newsgroups gmane.comp.java.jdom.general
Message-ID <[email protected]>
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

[email protected]

t: 720.524.4864

m: 303.809.9782

_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.