How to read xml files with namespaces

View: New views
2 Messages — Rating Filter:   Alert me  

How to read xml files with namespaces

by Brian Trezise :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

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@...

t: 720.524.4864

m: 303.809.9782

 


_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@...

RE: How to read xml files with namespaces

by Michael Kay :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
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");
 

 

 
 


From: jdom-interest-bounces@... [mailto:jdom-interest-bounces@...] On Behalf Of Brian Trezise
Sent: 06 August 2009 17:09
To: jdom-interest@...
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@...

t: 720.524.4864

m: 303.809.9782

 


_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@...