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?

Recommended Answers

All 5 Replies

You're trying to get a certain path from an xml file and then go to that direction an read all files that has??

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.

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?

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

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;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.