Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

root.getRootElement() is throwing an NullPointerException

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i want to read a xml file and store it in a String Variable.i have placed test.xml in WEB-INF and Webcontent.

SAXBuilder builder;
Document root = null;

try
{
builder = new SAXBuilder(
"org.apache.xerces.parsers.SAXParser" );

//i think test.xml is not found
root = builder.build("test.xml");
}
catch( Exception ee)
{}

The below code is throwing an Nullpointer exception
,since root has the value null.
context.put("root", root.getRootElement());

i guess the test.xml is not found in the above statement.
please tell me where to place the test.xml file or what changes i should make to make the above program work

Thanks in Advance
 
K Sathya Narayanan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i have found out the solution

Steps
1.Download the xercesImpl.jar file from the below link
http://www.java2s.com/Code/Jar/XML/DownloadxercesImpljar.htm

there are lot such jar file in the below website
http://www.java2s.com/Code/Jar/CatalogJar.htm

2.Add xercesImpl.jar file in your WEB-INF/lib folder

This will prevent the ClasscastException

Next you will get the ClassNotFoundException is removed , but , i now get the below exception
java.io.FileNotFoundException: D:\Eclipse3.2\eclipse\test.xml (The system cannot find the file specified)

The SAXBuilder is looking for test.xml in D:\Eclipse3.2\eclipse

so for time-being , you have to spcify the exact path in which the xml file is
located

for example if test.xml is in d:\projects\DataProjects

use the below code

builder = new SAXBuilder( "org.apache.xerces.parsers.SAXParser" );



String path="D:\\projects\\DataProjects\\";

root = builder.build(path+"\\test.xml");


Then the fileNotException will be eliminated
 
K Sathya Narayanan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

if(root!=null)
{
context.put("root", root.getRootElement());

System.out.println("Value ="+ root.getRootElement().getChild("document").getChild("properties")
.getChild("title").getText());

}
/*root.getRootElement().getChild("document").getChild("properties")
.getChild("title").getText()

is throwing the NullPointer Exception*/

when i use the above line in my java class it is throwing the below exception

java.lang.NullPointerException
[ January 08, 2008: Message edited by: K Sathya Narayanan ]
 
K Sathya Narayanan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i have found the solution

Thanks a Lot ,
i changed as below in java Code

builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser" );


String path="D:
Program Files
Apache Software Foundation
Tomcat 5.5
" +
"webapps\\ROOT\\SampleVelocityProject\\WebContent
WEB-INF";

root = builder.build(path+"
test.xml");

System.out.println("Value = #"+ root.getRootElement().getChild("properties").getChild("title").getText());

context.put("root", root);

In Template file , i displayed using the below code.

$root.getRootElement().getChild("properties").getChild("title").getText()
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic