xml parser + java/jsp question

i have a class that reads an xml file, manage to parse and return values correctly when compile and run in console, however, when running the class from the jsp file - it's all returning nulls.
Anyone have any ideas why?

LVL 39
Who is Participating?
CEHJConnect With a Mentor Commented:
I would suggest you do the following

a. Make an /xml directory at the root of your webapp
b. Load the xml file thus
String coursePath = "/xml/my.xml"; // (or whatever)
Document dom = db.parse(new InputSource(getClass().getResourceAsStream(coursePath));

This approach will work whether it's a webapp or an application in a jar
Probably not reading the file correctly. How are you reading it?
If it is returning correct values when you run it from console, but not as part of an application (jsp page), I think it could be a problem related to file paths. Just check if you are giving the right path to the xml file in your jsp.
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

blue-genieAuthor Commented:
we're quite sure its finding the file, we did a if (fileExists) - which returns true

in terms of how we are reading it -
reading it by gettting elements and nodes.
blue-genieAuthor Commented:
here's the method...

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(coursePath);
Element docEle = dom.getDocumentElement();
NodeList nl = docEle.getElementsByTagName("lom:general");
if (nl != null && nl.getLength() > 0) {
      for (int i = 0; i < nl.getLength(); i++) {
            Element el = (Element) nl.item(i);
                      NodeList children = el.getChildNodes();

      for (int x = 0; x < children.getLength(); x++) {
      NodeList innerChildren = children.item(x).getChildNodes();
      for (int y = 0; y < innerChildren.getLength(); y++) {
      String value = innerChildren.item(y)+"";
      //over here value is null when called from the jsp but contains the values I want when run from the console      }
            } catch (Exception e) {
                  System.out.println("parseManifestFile() " + e);

here's a bit of the xml...

<?xml version="1.0" encoding="UTF-8"?>
<!--This is a Reload version 1.3.2 Beta2_c SCORM 2004 Content Package document-->
<!--Spawned from the Reload Content Package Generator - http://www.reload.ac.uk-->
<manifest xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" xmlns:lom="http://ltsc.ieee.org/xsd/LOM" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_v1p3" xmlns:imsss="http://www.imsglobal.org/xsd/imsss" xmlns:adlseq="http://www.adlnet.org/xsd/adlseq_v1p3" xmlns:adlnav="http://www.adlnet.org/xsd/adlnav_v1p3" identifier="MANIFEST-AADLC-SCORM2004ExamplePkg" xsi:schemaLocation="http://www.imsglobal.org/xsd/imscp_v1p1 imscp_v1p1.xsd http://ltsc.ieee.org/xsd/LOM lom.xsd http://www.adlnet.org/xsd/adlcp_v1p3 adlcp_v1p3.xsd http://www.imsglobal.org/xsd/imsss imsss_v1p0.xsd http://www.adlnet.org/xsd/adlseq_v1p3 adlseq_v1p3.xsd http://www.adlnet.org/xsd/adlnav_v1p3 adlnav_v1p3.xsd" version="1.3">
    <schema>ADL SCORM</schema>
    <schemaversion>CAM 1.3</schemaversion>
                        <lom:string language="en-US">Title for the Package</lom:string>
                        <lom:string language="hebrew">kus emak!</lom:string>
                        <lom:string language="en-US">Textual Description</lom:string>
                        <lom:string language="en-US">keyword1</lom:string>
                        <lom:string language="en-US">keyword2</lom:string>
                        <lom:string language="en-US">this is my coverage</lom:string>
                        <lom:string language="en-US">this is my structure</lom:string>
                        <lom:string language="en-US">this is my aggregation</lom:string>

we're trying to get the stuff between the general nodes.

ta peeps.
mukundha_expertConnect With a Mentor Commented:
IT may be because the file path is wrong.

If you are trying to access a file from the JSP , it will take the root directory as the bin folder of ur container.

suppose if you are using tomcat as ur container then the jsp will search in the tomcat/bin folder for the file.

to know the exact location where it searches for a file, try to create a FileOutputStream and check where the file is being created. it will be in your server directory only.


FileOutputStream f = new FileOutputStream("Jack.txt");

aftre that search jack.txt in ur system. it is the place where you have to place your xml.

blue-genieAuthor Commented:
wer're using absolute paths. ie. c:\ and we've run checks in the jsp to see that its pointing to the right file.
runa_paathakConnect With a Mentor Commented:
If it's working fine when you run from the console, it means there is no problem with the logic. The problem most likely has to do with how you are running it from the jsp. I think it would help if you could post the relevant parts of your jsp.
blue-genieAuthor Commented:
CEHJ  - we get a malformed url exception
any idea?
blue-genieAuthor Commented:
cehj, since we're using absolute paths, we know the file can be found, otherwise it would be throwing a fileNOt found exception? yea?
 CEHJ  - we get a malformed url exception
any idea?

Please post stack trace
blue-genieAuthor Commented:

        at java.net.URL.<init>(URL.java:601)
        at java.net.URL.<init>(URL.java:464)
        at java.net.URL.<init>(URL.java:413)
        at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:968)
        at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:184)

at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:798)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:148)

        at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:250)

at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:292)
        at ManifestCourseReader.parseManifestFile(ManifestCourseReader.java:120)
        at ManifestCourseReader.<init>(ManifestCourseReader.java:53)
        at ManifestCourseReader.main(ManifestCourseReader.java:313)
objectsConnect With a Mentor Commented:
> Document dom = db.parse(coursePath);


Document dom = db.parse(new File(coursePath));
blue-genieAuthor Commented:
hi, the guy who's working with me on this is off on study leave for a while, when he gets back i'll have him take a look, i haven't abandoned the issue :-)
thanks so long for your efforts objects.
blue-genieAuthor Commented:
ok, objects that didn't help.
but he's used a different xml parser now instead of the ADL one and he seems to have solved it.
so for ur efforts, though this hasn't been resolved i'm gonna split the points and assign a B.
thanks ppl.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.