• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

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?

ta
blu.
0
blue-genie
Asked:
blue-genie
  • 8
  • 4
  • 2
  • +2
4 Solutions
 
CEHJCommented:
Probably not reading the file correctly. How are you reading it?
0
 
runa_paathakCommented:
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.
0
 
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 -
documentbuilderfactory
javac.xml.parsers.*
reading it by gettting elements and nodes.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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">
  <metadata>
    <schema>ADL SCORM</schema>
    <schemaversion>CAM 1.3</schemaversion>
      <lom:lom>
            <lom:general>
                  <lom:title>
                        <lom:string language="en-US">Title for the Package</lom:string>
                        <lom:string language="hebrew">kus emak!</lom:string>
                  </lom:title>
                  <lom:description>
                        <lom:string language="en-US">Textual Description</lom:string>
                  </lom:description>
                  <lom:keyword>
                        <lom:string language="en-US">keyword1</lom:string>
                  </lom:keyword>
                  <lom:keyword>
                        <lom:string language="en-US">keyword2</lom:string>
                  </lom:keyword>
                  <lom:coverage>
                        <lom:string language="en-US">this is my coverage</lom:string>
                  </lom:coverage>
                  <lom:structure>
                        <lom:string language="en-US">this is my structure</lom:string>
                  </lom:structure>
                  <lom:aggregationLevel>
                        <lom:string language="en-US">this is my aggregation</lom:string>
                  </lom:aggregationLevel>
            </lom:general>
      </lom:lom>


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

ta peeps.
0
 
mukundha_expertCommented:
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.

  eg://

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.

cheers,
Mukundh
0
 
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.
0
 
runa_paathakCommented:
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.
0
 
CEHJCommented:
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
0
 
blue-genieAuthor Commented:
CEHJ  - we get a malformed url exception
any idea?
0
 
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?
0
 
CEHJCommented:
>>
 CEHJ  - we get a malformed url exception
any idea?
>>

Please post stack trace
0
 
blue-genieAuthor Commented:

java.net.MalformedURLException
        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

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)
0
 
objectsCommented:
> Document dom = db.parse(coursePath);

try:

Document dom = db.parse(new File(coursePath));
0
 
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.
0
 
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.
0
 
CEHJCommented:
:-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now