| | |
Accessing classes from xml settings
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2009
Posts: 13
Reputation:
Solved Threads: 0
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?
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.
•
•
Join Date: Apr 2008
Posts: 972
Reputation:
Solved Threads: 145
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.
•
•
Join Date: Apr 2008
Posts: 972
Reputation:
Solved Threads: 145
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!
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
•
•
Join Date: Nov 2007
Posts: 16
Reputation:
Solved Threads: 1
I used SAX and Xerces to create a XMLReader, and it works very well
Java Syntax (Toggle Plain Text)
import java.io.IOException; import java.util.Hashtable; import org.apache.xerces.parsers.*; import org.w3c.dom.*; import org.xml.sax.SAXException; /** * * @author Daniel */ public class XMLParser { //Variables. private String filePath; private Hashtable<String, String> configValues; //Objects to manipulate the XML file. private DOMParser oParser; private Document oDocument; private Element oElement; //Returns any request that is in the //XML document. public String getValue(String key){ return configValues.get(key); } //Loads all the dbParams of the XML Document. private void loadConfigValues(){ NodeList config = oDocument.getElementsByTagName("dbParam"); for(int i=0; i < config.getLength(); i++){ oElement = (Element)config.item(i); configValues.put(oElement.getAttribute("name"),oElement.getAttribute("value")); } } //Loads the parser and the document. public XMLParser(String filePath)throws IOException, SAXException{ setFilePath(filePath); initXML(); initConfigValues(); } private void initXML()throws IOException, SAXException{ oParser = new DOMParser(); oParser.parse(getFilePath()); initDocument(); } private void initDocument(){ oDocument = oParser.getDocument(); } private void initConfigValues(){ configValues = new Hashtable<String, String>(); loadConfigValues(); } //Get and Set of the filePath private void setFilePath(String file){ this.filePath = file; } private String getFilePath(){ return filePath; } }
![]() |
Other Threads in the Java Forum
- Previous Thread: Drag and Drop components to tab
- Next Thread: Reading a txt file into a swing component
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class classes client code compile compiler component database developmenthelp eclipse error exception fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac map method methods mobile myregfun netbeans newbie notdisplaying number online page print problem program programming project qt recursion scanner screen server set singleton size sms sort spamblocker sql string swing system template textfields threads time title tree tutorial-sample update variablebinding windows working xor





