We help IT Professionals succeed at work.

Java DOM error, returning null instead of correct value for .getTextContent();

cmgtech
cmgtech asked
on
1,640 Views
Last Modified: 2008-02-01
So, the statement at line 19:

System.out.println(test[j][1] +", " + test[j][2] +", " + test[j][3] +", " + test[j][4] +", " + test[j][5] +", " + test[j][6] +", " + test[j][7] +", a");

returns all nulls.  it used to return the text values of the nodes that are parsed below.  Not when i .getTextContent(), I get errors.

It used to work fine, all of a sudden it returns just nulls across the board.  if i use a .getNoneName(), it reurns the correct node.  I have no idea what i did, but something has gone wrong.

i'm dying here.

Thanks in advance,

mark.



==============================code======================


import org.w3c.dom.*;
import java.net.URL;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;


public class blah {
      public static void main(String[] args) throws Exception {
            long st = System.currentTimeMillis();
            while (System.currentTimeMillis() - st < 1000){
                  String s = "f";
                  s.trim();
            }
            Document dom = getDOM(args[0]);
            //Document dom = getDOM("http://del.icio.us/rss/tag/rdf");
            String[][] test = flattenDOM(dom);
            for (int j = 0; j <= test.length-1; j++) {
                  System.out.println(test[j][1] +", " + test[j][2] +", " + test[j][3] +", " + test[j][4] +", " + test[j][5] +", " + test[j][6] +", " + test[j][7] +", a");
            }
      }
      
      public static Document getDOM(String url) throws Exception{
         URL yahoo = new URL(url);
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         factory.setValidating(false);
         Document doc = factory.newDocumentBuilder().parse(new InputSource(yahoo.openStream()));
         return doc;
          }
      
      public static String[][] flattenDOM(Document dom) {
            //NodeList nl = dom.getElementsByTagName("title");
            NodeList nl = dom.getElementsByTagName("item");
            NodeList tags = dom.getElementsByTagName("rdf:li");
            String[][] vars = new String[tags.getLength() -nl.getLength()][8];
            int tagcount = 0;
            for (int i = 0; i <= nl.getLength() - 1; i++) {
                  String title = new String("");
                  String link = new String("");
                  String desc = new String("");
                  String creator = new String("");
                  String postdate = new String("");
                  String subject = new String("");
                  String tag = new String("");
                  Node n = nl.item(i);
                  for (int j = 0; j <= n.getChildNodes().getLength() - 1; j++){
                        Node n2 = n.getChildNodes().item(j);
                        if (j % 2 == 1){
                              if (n2.getNodeName() == "title") title = n2.getTextContent();
                              if (n2.getNodeName() == "link") link = n2.getTextContent();
                              if (n2.getNodeName() == "description") desc = n2.getTextContent();
                              if (n2.getNodeName() == "dc:creator") creator = n2.getTextContent();
                              if (n2.getNodeName() == "dc:date") postdate = n2.getTextContent();
                              if (n2.getNodeName() == "dc:subject") subject = n2.getTextContent();
                              if (n2.getNodeName() == "taxo:topics"){
                                    //System.out.println((n2.getChildNodes().item(1).getChildNodes().getLength() - 1)/2);
                                    for (int k = 1; k <= n2.getChildNodes().item(1).getChildNodes().getLength()-1; k++) {
                                          Node n3 = n2.getChildNodes().item(1).getChildNodes().item(k);
                                          if (n3.getNodeName() == "rdf:li") tag = n2.getTextContent();
                                          if (k % 2 == 1){
                                                tag = n3.getAttributes().item(0).getNodeValue();
                                                vars[tagcount][1] = title;
                                                vars[tagcount][2] = link;
                                                vars[tagcount][3] = desc;
                                                vars[tagcount][4] = creator;
                                                vars[tagcount][5] = postdate;
                                                vars[tagcount][6] = subject;
                                                vars[tagcount][7] = tag;
                                                tagcount = tagcount + 1;
                                          }
                                    }
                              }
                        }
                  }
            }
            return vars;
      }
}
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
All those tests should be

 if ("title".equals(n2.getNodeName())

and similar
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>if (n2.getNodeName().equals("title")) title = n2.getTextContent();

..........

Why?
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
u could use a map to store the values, you would need lots of ifs then

attributes.put(n2.getNodeName(), n2.getTextContent());


and if you are using ifs, it should be if/then/else

if (n2.getNodeName().equals("title")) title = n2.getTextContent();
else if (n2.getNodeName().equals("link")) link = n2.getTextContent();
else if (n2.getNodeName().equals("description")) desc = n2.getTextContent();
else if (n2.getNodeName().equals("dc:creator")) creator = n2.getTextContent();
else if (n2.getNodeName().equals("dc:date")) postdate = n2.getTextContent();
else if (n2.getNodeName().equals("dc:subject")) subject = n2.getTextContent();
else if (n2.getNodeName().equals("taxo:topics")){

Author

Commented:
So, the below code, keeps returning null for getNodeName() and when I try getTextContent(), I get and error:
Exception in thread "main" java.lang.AbstractMethodError: org.apache.xerces.dom.DeferredTextImpl.getTextContent()Ljava/lang/String;
      at XPathHelper.parseRDF(XPathHelper.java:36)
      at XPathHelper.getDOM(XPathHelper.java:25)
      at XPathHelper.main(XPathHelper.java:17)
0. http://www.xulplanet.com/tutorials/mozsdk/rdfstart.php

can;t figure it out....


import java.io.*;
import java.net.URL;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import java.sql.DriverManager;
import org.w3c.dom.*;
import java.net.URL;
import java.sql.*;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;


public class XPathHelper {
      public static void main(String[] args) throws IOException, ParserConfigurationException, org.xml.sax.SAXException, Exception {
            getDOM();
      }
      
      public static void getDOM() throws Exception{
               URL yahoo = new URL("http://del.icio.us/rss/tag/rdf");
               DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
               factory.setValidating(false);
               Document dom = factory.newDocumentBuilder().parse(new InputSource(yahoo.openStream()));
               parseRDF(dom);
               }
               
      public static void parseRDF(Document dom) {
            NodeList sections = dom.getElementsByTagName("item");
            for (int i = 0; i < sections.getLength(); i++){
                  Node n = sections.item(i);
                  String item = n.getAttributes().item(0).getNodeValue();
                  System.out.println(i + ". " +item);
                  for (int j = 0; j < n.getChildNodes().getLength(); j++){
                        String nodeName = n.getChildNodes().item(j).getNodeName();
                        String nodeValue = n.getChildNodes().item(j).getNodeValue();
                        if (nodeName != "#text") {
                              System.out.println("--" + nodeName + ": " + nodeValue + ", ");
                        }
                  }
            }
            System.out.println("out");
      }
}

=====output======
0. http://www.xulplanet.com/tutorials/mozsdk/rdfstart.php
--title: null,
--link: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
1. http://weblog.scifihifi.com/2005/08/05/meta-tags-the-poor-mans-rdf
--title: null,
--link: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
2. http://geobloggers.com/archives/2007/01/24/offtopic-ish-flickr-ramps-up-triple-tag-support/
--title: null,
--link: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
3. http://www.soundadvice.id.au/blog/2007/01/26/#rdfREST3
--title: null,
--link: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
4. http://scholar.google.com/scholar%3Fq%3Drdf+owl+text+analysis%26hl%3Den%26client%3Dfirefox-a%26channel%3Ds%26rls%3Dorg.mozilla:en-US:official%26hs%3DzDb%26oi%3Dscholart
--title: null,
--link: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
5. http://pellet.owldl.com/
--title: null,
--link: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
6. http://www.w3.org/2001/sw/
--title: null,
--link: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
7. http://librdf.org/docs/ruby.html
--title: null,
--link: null,
--description: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
8. http://scott-tabar.blogspot.com/2006/10/rdf-owl-dl-and-other-semantic-web.html
--title: null,
--link: null,
--description: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
9. http://www.activerdf.org/
--title: null,
--link: null,
--description: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
10. http://weblog.scifihifi.com/2005/08/05/meta-tags-the-poor-mans-rdf
--title: null,
--link: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
11. http://www.flickr.com/groups/api/discuss/72157594497877875/
--title: null,
--link: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
12. http://www.aaronland.info/weblog/2007/01/24/bacon/
--title: null,
--link: null,
--description: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
13. http://www.flickr.com/groups/api/discuss/72157594497877875/
--title: null,
--link: null,
--description: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
14. http://radar.oreilly.com/archives/2007/01/flickr_launches.html
--title: null,
--link: null,
--description: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
15. http://dev.openguides.org/wiki/RDF%20Workshop
--title: null,
--link: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
16. http://www.computerbytesman.com/privacy/blair.htm
--title: null,
--link: null,
--description: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
17. http://infomesh.net/2001/swintro/
--title: null,
--link: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
18. http://blogspace.com/rdf/SwartzHendler
--title: null,
--link: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
19. http://logicerror.com/semanticWeb-long
--title: null,
--link: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
20. http://rdfweb.org/people/damian/RDFAuthor/
--title: null,
--link: null,
--description: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
21. http://logicerror.com/semanticWeb-long
--title: null,
--link: null,
--description: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
22. http://esw.w3.org/topic/ConverterToRdf
--title: null,
--link: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
23. http://metamorphoses.sourceforge.net/#documentation
--title: null,
--link: null,
--description: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
24. http://weblog.scifihifi.com/2005/08/05/meta-tags-the-poor-mans-rdf
--title: null,
--link: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
25. http://www.kanzaki.com/works/2007/pub/0125intap.html
--title: null,
--link: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
26. http://simile.mit.edu/
--title: null,
--link: null,
--description: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
27. http://www.kanzaki.com/docs/sw/rdf-calendar.html
--title: null,
--link: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
28. http://geobloggers.com/archives/2007/01/24/offtopic-ish-flickr-ramps-up-triple-tag-support/
--title: null,
--link: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
29. http://www.flickr.com/groups/api/discuss/72157594497877875/
--title: null,
--link: null,
--description: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
30. http://mindraider.sourceforge.net/screenshots.html
--title: null,
--link: null,
--dc:creator: null,
--dc:date: null,
--dc:subject: null,
--taxo:topics: null,
out

===actual rdf (abbreviated)=====
<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:cc="http://web.resource.org/cc/" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:syn="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/">
      <channel rdf:about="http://del.icio.us/tag/rdf">
            <title>del.icio.us/tag/rdf</title>
            <link>http://del.icio.us/tag/rdf</link>
            <description/>
            <items>
                  <rdf:Seq>
                        <rdf:li rdf:resource="http://www.soundadvice.id.au/blog/2007/01/26/#rdfREST3"/>
                        <rdf:li rdf:resource="http://scholar.google.com/scholar%3Fq%3Drdf+owl+text+analysis%26hl%3Den%26client%3Dfirefox-a%26channel%3Ds%26rls%3Dorg.mozilla:en-US:official%26hs%3DzDb%26oi%3Dscholart"/>
                        <rdf:li rdf:resource="http://pellet.owldl.com/"/>
                        <rdf:li rdf:resource="http://www.w3.org/2001/sw/"/>
                        <rdf:li rdf:resource="http://librdf.org/docs/ruby.html"/>
                        <rdf:li rdf:resource="http://scott-tabar.blogspot.com/2006/10/rdf-owl-dl-and-other-semantic-web.html"/>
                        <rdf:li rdf:resource="http://www.activerdf.org/"/>
                        <rdf:li rdf:resource="http://weblog.scifihifi.com/2005/08/05/meta-tags-the-poor-mans-rdf"/>
                        <rdf:li rdf:resource="http://www.flickr.com/groups/api/discuss/72157594497877875/"/>
                        <rdf:li rdf:resource="http://www.aaronland.info/weblog/2007/01/24/bacon/"/>
                        <rdf:li rdf:resource="http://www.flickr.com/groups/api/discuss/72157594497877875/"/>
                        <rdf:li rdf:resource="http://radar.oreilly.com/archives/2007/01/flickr_launches.html"/>
                        <rdf:li rdf:resource="http://dev.openguides.org/wiki/RDF%20Workshop"/>
                        <rdf:li rdf:resource="http://www.computerbytesman.com/privacy/blair.htm"/>
                        <rdf:li rdf:resource="http://infomesh.net/2001/swintro/"/>
                        <rdf:li rdf:resource="http://blogspace.com/rdf/SwartzHendler"/>
                        <rdf:li rdf:resource="http://logicerror.com/semanticWeb-long"/>
                        <rdf:li rdf:resource="http://rdfweb.org/people/damian/RDFAuthor/"/>
                        <rdf:li rdf:resource="http://logicerror.com/semanticWeb-long"/>
                        <rdf:li rdf:resource="http://esw.w3.org/topic/ConverterToRdf"/>
                        <rdf:li rdf:resource="http://metamorphoses.sourceforge.net/#documentation"/>
                        <rdf:li rdf:resource="http://weblog.scifihifi.com/2005/08/05/meta-tags-the-poor-mans-rdf"/>
                        <rdf:li rdf:resource="http://www.kanzaki.com/works/2007/pub/0125intap.html"/>
                        <rdf:li rdf:resource="http://simile.mit.edu/"/>
                        <rdf:li rdf:resource="http://www.kanzaki.com/docs/sw/rdf-calendar.html"/>
                        <rdf:li rdf:resource="http://geobloggers.com/archives/2007/01/24/offtopic-ish-flickr-ramps-up-triple-tag-support/"/>
                        <rdf:li rdf:resource="http://www.flickr.com/groups/api/discuss/72157594497877875/"/>
                        <rdf:li rdf:resource="http://mindraider.sourceforge.net/screenshots.html"/>
                        <rdf:li rdf:resource="http://weblog.scifihifi.com/2005/08/05/meta-tags-the-poor-mans-rdf"/>
                        <rdf:li rdf:resource="http://rdf2java.opendfki.de/cgi-bin/trac.cgi"/>
                        <rdf:li rdf:resource="http://infomesh.net/2001/08/swtips"/>
                  </rdf:Seq>
            </items>
      </channel>
      <item rdf:about="http://www.soundadvice.id.au/blog/2007/01/26/#rdfREST3">
            <title>Sound advice - RDF and the Semantic Web - Are we there, yet?</title>
            <link>http://www.soundadvice.id.au/blog/2007/01/26/#rdfREST3</link>
            <dc:creator>jaredhanson</dc:creator>
            <dc:date>2007-01-27T01:15:18Z</dc:date>
            <dc:subject>rdf rest toread</dc:subject>
            <taxo:topics>
                  <rdf:Bag>
                        <rdf:li resource="http://del.icio.us/tag/toread"/>
                        <rdf:li resource="http://del.icio.us/tag/rdf"/>
                        <rdf:li resource="http://del.icio.us/tag/rest"/>
                  </rdf:Bag>
            </taxo:topics>
      </item>
      <item rdf:about="http://scholar.google.com/scholar%3Fq%3Drdf+owl+text+analysis%26hl%3Den%26client%3Dfirefox-a%26channel%3Ds%26rls%3Dorg.mozilla:en-US:official%26hs%3DzDb%26oi%3Dscholart">
            <title>rdf owl text analysis - Google Scholar</title>
            <link>http://scholar.google.com/scholar%3Fq%3Drdf+owl+text+analysis%26hl%3Den%26client%3Dfirefox-a%26channel%3Ds%26rls%3Dorg.mozilla:en-US:official%26hs%3DzDb%26oi%3Dscholart</link>
            <dc:creator>lacfront</dc:creator>
            <dc:date>2007-01-27T00:38:47Z</dc:date>
            <dc:subject>analysis owl rdf text</dc:subject>
            <taxo:topics>
                  <rdf:Bag>
                        <rdf:li resource="http://del.icio.us/tag/owl"/>
                        <rdf:li resource="http://del.icio.us/tag/rdf"/>
                        <rdf:li resource="http://del.icio.us/tag/text"/>
                        <rdf:li resource="http://del.icio.us/tag/analysis"/>
                  </rdf:Bag>
            </taxo:topics>
      </item>
      <item rdf:about="http://pellet.owldl.com/">
            <title>Pellet: An Open Source OWL-DL Reasoner in Java</title>
            <link>http://pellet.owldl.com/</link>
            <dc:creator>trina.myers</dc:creator>
            <dc:date>2007-01-27T00:14:05Z</dc:date>
            <dc:subject>inference logic ontology owl pellet rdf reasoner reasoning semantic semanticweb</dc:subject>
            <taxo:topics>
                  <rdf:Bag>
                        <rdf:li resource="http://del.icio.us/tag/inference"/>
                        <rdf:li resource="http://del.icio.us/tag/pellet"/>
                        <rdf:li resource="http://del.icio.us/tag/semanticweb"/>
                        <rdf:li resource="http://del.icio.us/tag/ontology"/>
                        <rdf:li resource="http://del.icio.us/tag/reasoner"/>
                        <rdf:li resource="http://del.icio.us/tag/reasoning"/>
                        <rdf:li resource="http://del.icio.us/tag/semantic"/>
                        <rdf:li resource="http://del.icio.us/tag/logic"/>
                        <rdf:li resource="http://del.icio.us/tag/owl"/>
                        <rdf:li resource="http://del.icio.us/tag/rdf"/>
                  </rdf:Bag>
            </taxo:topics>
      </item>
      <item rdf:about="http://www.w3.org/2001/sw/">
            <title>W3C Semantic Web Activity</title>
            <link>http://www.w3.org/2001/sw/</link>
            <dc:creator>martinn</dc:creator>
            <dc:date>2007-01-26T23:56:16Z</dc:date>
            <dc:subject>rdf semantic semantic_web semanticweb w3c web</dc:subject>
            <taxo:topics>
                  <rdf:Bag>
                        <rdf:li resource="http://del.icio.us/tag/w3c"/>
                        <rdf:li resource="http://del.icio.us/tag/semantic"/>
                        <rdf:li resource="http://del.icio.us/tag/web"/>
                        <rdf:li resource="http://del.icio.us/tag/semanticweb"/>
                        <rdf:li resource="http://del.icio.us/tag/semantic_web"/>
                        <rdf:li resource="http://del.icio.us/tag/rdf"/>
                  </rdf:Bag>
            </taxo:topics>
      </item>
...
...
...
...

Author

Commented:
also, kawas: i am looking at jena. it looks good.  thanks for the tip.

this is something i am trying to figure out quickly though, and being new to java and kind of an idiot, i dont have the time to pick it up right now.  if i am not too discouraged with myself after this project, i think it will be very useful in future development projects.

thanks,

mark
CERTIFIED EXPERT
Top Expert 2016

Commented:
Can you make absolutely sure that you've pasted the exception precisely?

Author

Commented:
it ended up being a java configuration error.  i am learning the hard way that shortcuts dont al ways work well when you're new.

thanks for following up.

mark
CERTIFIED EXPERT
Top Expert 2016

Commented:
For the benefit of those coming to this thread later, the 'improved' version of the suggestion i posted at http:Q_22137657.html#a18402588 that was accepted as an assisted answer differs from the original only in that it will potentially produce a NullPointerException

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.