Sto usando il parser Dom per leggere da un file di configurazione xml del genere:
<root>
<certificate>
<type>tizio</type>
...
</certificate>
<root>
Usando questo codice:
//reading from xml files
try
{
//reading from XML config file general
DocumentBuilderFactory factory1 = DocumentBuilderFactory.newInstance();
factory1.setValidating(false);
//create the builder and parse the file
Document doc1 = factory1.newDocumentBuilder().parse(new FileInputStream(System.getProperty("user.dir")+ "/" + configfilepath));
WorkingDirectory =
doc1.getDocumentElement().getElementsByTagName("Wo rkingDirectory").item(0).getFirstChild().getNodeVa lue().trim();
PathCertificatesDB = doc1.getDocumentElement().getElementsByTagName("Ce rtificatesDatabase").item(0).getFirstChild().getNo deValue().trim();
System.out.println(PathCertificatesDB + " " + WorkingDirectory);
/* //reading from the XML Certificates Database
DocumentBuilderFactory factory2 = DocumentBuilderFactory.newInstance();
factory2.setValidating(false);
Document doc2 = factory2.newDocumentBuilder().parse(new FileInputStream(PathCertificatesDB));
//normalization of the document Dom trough the interface Document
//doc2.getDocumentElement().normalize();
//construction of the three, the nl contains the list of all the element Certificates
NodeList nl = doc2.getDocumentElement().getElementsByTagName("Ce rtificate");
Element root = doc2.getDocumentElement();
Node Valorenodo = root.getFirstChild();
//perhaps can use NodeMaNamedNodeMap
//cicle on all the element with tagname certificate
for (int i =0; i<nl.getLength(); i++)
{
Node subnode1 = Valorenodo.getFirstChild();
kind = subnode1.getNodeValue();
subnode1 = subnode1.getNextSibling();
pathcertificate = subnode1.getNodeValue();
subnode1 = subnode1.getNextSibling();
value = subnode1.getNodeValue();
listModel.addElement(kind + " is a " + value + "-" + pathcertificate);
int j=nl.getLength()-1;
if (i<j)
{
Valorenodo = Valorenodo.getNextSibling();
}
}*/
}
//management of exception
catch (IOException ioe)
{
System.err.println("Input/Output error: " + ioe.getMessage());
System.exit(1);
}
catch (SAXParseException spe)
{
System.err.println("Parsing exception for entity " + spe.getPublicId() + " at line: " + spe.getLineNumber() + " column: " + spe.getColumnNumber());
System.exit(1);
}
catch (SAXException se)
{
System.err.println("General SAX exception: " + se.getMessage());
System.exit(1);
}
catch (ParserConfigurationException pce)
{
System.err.println("General SAX exception: " + pce.getMessage());
System.exit(1);
}
catch (FactoryConfigurationError fce)
{
System.err.println("Configuration error: " + fce.getMessage());
System.exit(1);
}
Che pero' mi da questo errore:
unreported exception javax.xml.parsers.ParserConfigurationException; must be caught or declared to be thrown alle righe dove istanzio i nuovi documenti doc1 e doc2.
Qualcuno saprebbe dirmi dove sbaglio?