Posted By:
Otis_Gospodnetic
Posted On:
Saturday, November 17, 2001 11:16 AM
Hello,
Unless I am misunderstanding your question, this requires no 'special' coding.
You can read the file with
java.io.File class, parse is with something like
Xerces-J from Apache, instantiate your
Map and simply put elements in it like you normally put them in the
Map.
You then just have to expose this
Map to the rest of the code (e.g. make it public, or wrap it in a class that has accessor methods that get values from the
Map, etc.)
Here is some code that will help you with reading the XML file.
/**
* Main.
*
* @param args command line arguments
*/
public static void main(String[] args)
{
if (args.length < 1)
{
System.err.println("Feed me some arguments...");
System.exit(0);
}
for (int i=0; i < args.length; i++)
{
File file = new File(args[i]);
// optionally check that the file exists, is readable, is not a directory, etc.
FileInputStream fis = null;
try
{
fis = new FileInputStream(file);
}
catch (FileNotFoundException fnfe)
{
System.out.println("File " + args[i] + " can't be read.");
if (fis != null)
{
try
{
fis.close();
}
catch (IOException ioe)
{
System.out.println("File " + args[i] + " can't be closed.");
}
}
// go to next file
continue;
}
byte[] buffer = new byte[4096];
int bytesRead;
//StringBuffer xmlStrBuffer = new StringBuffer();
String xmlStr = "";
try
{
while ((bytesRead = fis.read(buffer)) != -1)
{
//xmlStrBuffer.append(buffer, 0, bytesRead);
xmlStr = xmlStr.concat(new String(buffer, 0, bytesRead));
}
}
catch (IOException ioe)
{
if (fis != null)
{
try
{
fis.close();
}
catch (IOException ioe2)
{
System.out.println("Error reading file " + args[i]);
}
}
// go to next file
continue;
}
try
{
//System.out.println(xmlStr);
// CALL Xerces-J HERE
}
catch (org.xml.sax.SAXException se)
{
System.err.println("org.xml.sax.SAXException thrown");
se.printStackTrace();
}