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

Brett McLaughlin brett.mclaughlin at lutris.com
Mon Aug 28 08:13:05 PDT 2000


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) {
>     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]));
> 
>         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@yourhost.com

-- 
Brett McLaughlin, Enhydra Strategist
Lutris Technologies, Inc. 
1200 Pacific Avenue, Suite 300 
Santa Cruz, CA 95060 USA 
http://www.lutris.com
http://www.enhydra.org



More information about the jdom-interest mailing list