[jdom-interest] Problems with calls to getChild and getAttribute methods...

Bruce Hacker bhacker at automark.net
Mon Aug 28 10:35:32 PDT 2000


	I've tried making a call to getChild("Title", ns) but still get the same
error.  The only way that I've been able to successfully retrieve elements
is by calling getChilden() and then iterating through the returned list.

Regards
-Bruce


-----Original Message-----

From: jdom-interest-admin at jdom.org
[mailto:jdom-interest-admin at jdom.org]On Behalf Of Brett McLaughlin
Sent: Monday, August 28, 2000 11:13 AM
Cc: jdom-interest at jdom.org
Subject: Re: [jdom-interest] Problems with calls to getChild and
getAttribute methods...


Bruce Hacker wrote:
>
>         I recently started using/evaluating JDOM and am having a problem
getting
> results when I make calls to the getChild and getAttribute methods of
> elements.  The error, Java Code and XML document that I am using are
listed
> below.  Any assistance will be appreciated.  I have tried making calls to
> getChild several different ways (with and without the Namespace) but
always
> get the same error:

You need to do getChild("Title", ns), not getChild("JavaXML:Title").

There isn't any element named "JavaXML:Title", only one named "Title"
with the namespace associated with the "JavaXML" prefix.

>
> ------------------------------Error message-----------------------------
>
> org.jdom.NoSuchElementException: The element JavaXML:Title does not exist
> within the specified element.
>         at org.jdom.Element.getChild(Element.java, Compiled Code)
>         at org.jdom.Element.getChild(Element.java:733)
>         at SAXTest.main(SAXTest.java:46)
>
> -------------------------------Java Code--------------------------------
>
> import java.io.File;
> import java.io.IOException;
> import java.io.OutputStream;
>
> import org.jdom.Document;
> import org.jdom.Element;
> import org.jdom.Namespace;
> import org.jdom.JDOMException;
> import org.jdom.input.SAXBuilder;
>
> class SAXTest {
>
>   public static void main(String[] args) {
>8     if (args.length != 1) {
>       System.out.println("Usage: SAXTest [filename to parse]");
>       return;
>     }
>
>     try {
>         // Request document building without validation
>         SAXBuilder builder = new SAXBuilder(false);
>         Document doc = builder.build(new File(args[0]));
8>
>         System.out.println("Document successfully read\n");
>
>         /*********************************************************/
>
>         // Get the Root element of the document
>         Element root = doc.getRootElement();
>         Namespace ns = root.getNamespace();
>
>         // Get the first title element
>         Element title = root.getChild("JavaXML:Title");
>
>         System.out.println("Root element name: " + root.getName());
>         System.out.println("Namespace: " + root.getNamespacePrefix());
>         System.out.println("Namespace URI: " + root.getNamespaceURI());
>         System.out.println();
>         System.out.println("  Title element: " + title.getName());
>
>         /*********************************************************/
>
>     } catch (JDOMException e) {
>         e.printStackTrace();
>     }
>   }
> }
>
> --------------------------XML Document---------------------------------
>
> <?xml version='1.0'?>
>
> <!-- Comment out for now
>  <?xml-stylesheet href="XSL\JavaXML.html.xsl" type="text/xsl"?>
>  <?xml-stylesheet href="XSL\JavaXML.wml.xsl" type="text/xsl"
>                   media="wap"?>
>  <?cocoon-process type="xslt"?>
>  <!DOCTYPE JavaXML:Book SYSTEM "DTD\JavaXML.dtd">
> -->
>
> <!-- Java and XML -->
> <JavaXML:Book xmlns:JavaXML="http://www.oreilly.com/catalog/javaxml/">
>  <JavaXML:Title>Java and XML</JavaXML:Title>
>  <JavaXML:Contents>
>
>   <JavaXML:Chapter focus="XML">
>    <JavaXML:Heading>Introduction</JavaXML:Heading>
>    <JavaXML:Topic subSections="7">What Is It?</JavaXML:Topic>
>    <JavaXML:Topic subSections="3">How Do I Use It?</JavaXML:Topic>
>    <JavaXML:Topic subSections="4">Why should I Use It?</JavaXML:Topic>
>    <JavaXML:Topic subSections="0">What's Next?</JavaXML:Topic>
>   </JavaXML:Chapter>
>
>   <JavaXML:Chapter focus="XML">
>    <JavaXML:Heading>Creating XML</JavaXML:Heading>
>    <JavaXML:Topic subSections="0">An XML Document</JavaXML:Topic>
>    <JavaXML:Topic subSections="2">The Header</JavaXML:Topic>
>    <JavaXML:Topic subSections="6">The Content</JavaXML:Topic>
>    <JavaXML:Topic subSections="1">What's Next?</JavaXML:Topic>
>   </JavaXML:Chapter>
>
>   <JavaXML:Chapter focus="Java">
>    <JavaXML:Heading>Parsing XML</JavaXML:Heading>
>    <JavaXML:Topic subSections="3">Getting Prepared</JavaXML:Topic>
>    <JavaXML:Topic subSections="3">SAX Readers</JavaXML:Topic>
>    <JavaXML:Topic subSections="9">Content Handlers</JavaXML:Topic>
>    <JavaXML:Topic subSections="4">Error Handlers</JavaXML:Topic>
>    <JavaXML:Topic subSections="0">
>      A Better Way to Load a Parser
>    </JavaXML:Topic>
>    <JavaXML:Topic subSections="4">"Gotcha!"</JavaXML:Topic>
>    <JavaXML:Topic subSections="0">What's Next?</JavaXML:Topic>
>   </JavaXML:Chapter>
>
>   <JavaXML:SectionBreak/>
>
>   <JavaXML:Chapter focus="Java">
>    <JavaXML:Heading>Web Publishing Frameworks</JavaXML:Heading>
>    <JavaXML:Topic subSections="4">Selecting a Framework</JavaXML:Topic>
>    <JavaXML:Topic subSections="4">Installation</JavaXML:Topic>
>    <JavaXML:Topic subSections="3">
>      Using a Publishing Framework
>    </JavaXML:Topic>
>    <JavaXML:Topic subSections="2">XSP</JavaXML:Topic>
>    <JavaXML:Topic subSections="3">Cocoon 2.0 and Beyond</JavaXML:Topic>
>    <JavaXML:Topic subSections="0">What's Next?</JavaXML:Topic>
>   </JavaXML:Chapter>
>
>  </JavaXML:Contents>
>
> <!-- Comment out for now
>  <JavaXML:Copyright>&OReillyCopyright;</JavaXML:Copyright>
> -->
>
> </JavaXML:Book>
>
> Regards
>
> _______________________________________________
> To control your jdom-interest membership:
>
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com





---------------------------------------------------
                  Bruce Hacker
 Senior Software Engineer / Certified Oracle 8 DBA
                    AutoMark
             5789 B Arrowhead Drive
            Virginia Beach, VA 23462
            (757) 497-5025  ext. 325
             (bhacker at automark.net)
---------------------------------------------------







More information about the jdom-interest mailing list