943,961 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 456
  • Java RSS
Feb 18th, 2009
0

Accessing classes from xml settings

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ppp83 is offline Offline
13 posts
since Feb 2009
Feb 19th, 2009
0

Re: Accessing classes from xml settings

You're trying to get a certain path from an xml file and then go to that direction an read all files that has??
Reputation Points: 9
Solved Threads: 2
Newbie Poster
danielernesto is offline Offline
16 posts
since Nov 2007
Feb 19th, 2009
0

Re: Accessing classes from xml settings

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.
Featured Poster
Reputation Points: 1924
Solved Threads: 952
Posting Expert
JamesCherrill is offline Offline
5,788 posts
since Apr 2008
Feb 19th, 2009
0

Re: Accessing classes from xml settings

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ppp83 is offline Offline
13 posts
since Feb 2009
Feb 19th, 2009
0

Re: Accessing classes from xml settings

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
Featured Poster
Reputation Points: 1924
Solved Threads: 952
Posting Expert
JamesCherrill is offline Offline
5,788 posts
since Apr 2008
Feb 19th, 2009
0

Re: Accessing classes from xml settings

I used SAX and Xerces to create a XMLReader, and it works very well

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 9
Solved Threads: 2
Newbie Poster
danielernesto is offline Offline
16 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Drag and Drop components to tab
Next Thread in Java Forum Timeline: Reading a txt file into a swing component





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC