4 Replies - 421 Views - Last Post: 21 September 2011 - 10:41 PM Rate Topic: -----

#1 acll19  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 20-October 10

java + xml issue

Posted 21 September 2011 - 09:04 PM

Hi there. I have an issue when parsing an xml file with java. the thing is that when I call the method getElementsByTagName I get an empty NodeList. I don't know what the problem is.
here's the xml file:


<configuration>
<data-access>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<db-manger>mysql</db-manger>
<dbname>db_css</dbname>
<dblocation>localhost</dblocation>
<user>root</user>
<password>root</password>
</data-access>
</configuration>

and the code to parse it:

  File f = new File(cnfUri);
                if(f.exists()){
                    DocumentBuilderFactory dbFactory =
                        DocumentBuilderFactory.newInstance();
                    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                    Document doc = dBuilder.parse(cnfUri);                    
                    driver = doc.getElementsByTagName("driver-class").
                        item(0).getNodeValue();
                    dbName = doc.getElementsByTagName("dbname").item(0).
                            getNodeValue();
                    dbLocation = doc.getElementsByTagName("dblocation").
                            item(0).getNodeValue();
                    user = doc.getElementsByTagName("user").item(0).
                            getNodeValue();
                    password = doc.getElementsByTagName("password").item(0).
                            getNodeValue();
                    manager = doc.getElementsByTagName("db-manger").item(0).
                            getNodeValue();
                }



Is This A Good Question/Topic? 0
  • +

Replies To: java + xml issue

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: java + xml issue

Posted 21 September 2011 - 09:17 PM

May be doing

Document doc = dBuilder.parse(cnfUri);

you are parsing the String contained in cnfUri if you want to parse the actual file do

Document doc = dBuilder.parse(f);

I think, I am far from being a XML expert
Was This Post Helpful? 1
  • +
  • -

#3 acll19  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 20-October 10

Re: java + xml issue

Posted 21 September 2011 - 09:24 PM

didn't work, thanks for your help any way. I'll keep working on it.
Was This Post Helpful? 0
  • +
  • -

#4 acll19  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 20-October 10

Re: java + xml issue

Posted 21 September 2011 - 09:39 PM

I tried
driver = doc.getElementsByTagName("*")

but it returns a nodelist with zero size.

When I debug the code I can see that doc(my parsed xml document) has all the nodes.
Was This Post Helpful? 0
  • +
  • -

#5 acll19  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 20-October 10

Re: java + xml issue

Posted 21 September 2011 - 10:41 PM

I have found the problem
here's the fixed code:
   File f = new File(cnfUri);
                if(f.exists()){
                    DocumentBuilderFactory dbFactory =
                        DocumentBuilderFactory.newInstance();
                    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();       
                    Document doc = dBuilder.parse(f);                
                   
                    driver = doc.getElementsByTagName("driver-class").
                        item(0).getChildNodes().item(0).getNodeValue();

                    dbName = doc.getElementsByTagName("dbname").item(0).
                            getChildNodes().item(0).getNodeValue();

                    dbLocation = doc.getElementsByTagName("dblocation").
                            item(0).getChildNodes().item(0).getNodeValue();

                    user = doc.getElementsByTagName("user").item(0).
                            getChildNodes().item(0).getNodeValue();

                    password = doc.getElementsByTagName("password").item(0).
                            getChildNodes().item(0).getNodeValue();
                    
                    manager = doc.getElementsByTagName("db-manger").item(0).
                            getChildNodes().item(0).getNodeValue();
                }



I hope this can help those of you who have the same problem as I did.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1