Accessing classes from xml settings

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2009
Posts: 13
Reputation: ppp83 is an unknown quantity at this point 
Solved Threads: 0
ppp83 ppp83 is offline Offline
Newbie Poster

Accessing classes from xml settings

 
0
  #1
Feb 18th, 2009
Im new to this forum and the programming language that im currently using is java. And i think i might need some help, advice here, and hope this is the correct place for me to post my question. If certain question i ask are not supposed to be posted here, do let me know then. Thanks.

I've tried the example i found on the net regarding reading xml file in java, and now my question is, my xml file has a listing of the files path, how am i going to access every single file specified inside the xml, with the basedir? Once i get the full path of the file, how am i going to read the content of the file so that i can get the information that i need?

From what i observe from the examples, they use sax or document builder to read the xml file, which method is more encouraged?
Last edited by ppp83; Feb 18th, 2009 at 11:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 16
Reputation: danielernesto is an unknown quantity at this point 
Solved Threads: 1
danielernesto danielernesto is offline Offline
Newbie Poster

Re: Accessing classes from xml settings

 
0
  #2
Feb 19th, 2009
You're trying to get a certain path from an xml file and then go to that direction an read all files that has??
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Accessing classes from xml settings

 
0
  #3
Feb 19th, 2009
Solutions like SAX are designed to cope with the worst-case complexity of an XML file. You probably don't need anything so complex. Look at the file; maybe you'll see that the names you need are prefixed by something like (for example) "<file><String>" and the file name continues up to the next "<". If that's the case it's easier to scan the file yourself looking for those strings and parsing the file names yourself. Once you have a base dir and file name you can create a new File object (look it up in the API) and read it anyway you want.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 13
Reputation: ppp83 is an unknown quantity at this point 
Solved Threads: 0
ppp83 ppp83 is offline Offline
Newbie Poster

Re: Accessing classes from xml settings

 
0
  #4
Feb 19th, 2009
thanks for the explanation, thats a very constructive suggestion. Just another question, once i get the path of the file from the xml file, how to call the methods inside the class of the specified path?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Accessing classes from xml settings

 
0
  #5
Feb 19th, 2009
Very briefly, you use a ClassLoader. You create a new instance of ClassLoader and use that to load the class(es) from the file into the current java VM..
Google "Java ClassLoader".

Or, just use this
http://maexplorer.sourceforge.net/MA...assLoader.html
open source class!
Last edited by JamesCherrill; Feb 19th, 2009 at 8:29 am. Reason: Added link to existing solution
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 16
Reputation: danielernesto is an unknown quantity at this point 
Solved Threads: 1
danielernesto danielernesto is offline Offline
Newbie Poster

Re: Accessing classes from xml settings

 
0
  #6
Feb 19th, 2009
I used SAX and Xerces to create a XMLReader, and it works very well

  1. import java.io.IOException;
  2. import java.util.Hashtable;
  3. import org.apache.xerces.parsers.*;
  4. import org.w3c.dom.*;
  5. import org.xml.sax.SAXException;
  6.  
  7. /**
  8.  *
  9.  * @author Daniel
  10.  */
  11. public class XMLParser {
  12. //Variables.
  13. private String filePath;
  14. private Hashtable<String, String> configValues;
  15.  
  16. //Objects to manipulate the XML file.
  17. private DOMParser oParser;
  18. private Document oDocument;
  19. private Element oElement;
  20.  
  21.  
  22. //Returns any request that is in the
  23. //XML document.
  24. public String getValue(String key){
  25. return configValues.get(key);
  26. }
  27.  
  28. //Loads all the dbParams of the XML Document.
  29. private void loadConfigValues(){
  30. NodeList config = oDocument.getElementsByTagName("dbParam");
  31. for(int i=0; i < config.getLength(); i++){
  32. oElement = (Element)config.item(i);
  33. configValues.put(oElement.getAttribute("name"),oElement.getAttribute("value"));
  34. }
  35. }
  36.  
  37. //Loads the parser and the document.
  38. public XMLParser(String filePath)throws IOException, SAXException{
  39. setFilePath(filePath);
  40. initXML();
  41. initConfigValues();
  42. }
  43. private void initXML()throws IOException, SAXException{
  44. oParser = new DOMParser();
  45. oParser.parse(getFilePath());
  46. initDocument();
  47. }
  48. private void initDocument(){
  49. oDocument = oParser.getDocument();
  50. }
  51. private void initConfigValues(){
  52. configValues = new Hashtable<String, String>();
  53. loadConfigValues();
  54. }
  55.  
  56. //Get and Set of the filePath
  57. private void setFilePath(String file){
  58. this.filePath = file;
  59. }
  60. private String getFilePath(){
  61. return filePath;
  62. }
  63. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC