Guys, please tell me how to specify the path to the file so that it does not swear?
FileNotFoundException
public boolean changeValuesInFiles () {
for (File f: newFilesList) {
try {
String filepath = "file: ///" + f.getAbsolutePath ();
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance ();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder ();
Document doc = docBuilder.parse (filepath);
Node pointOfSale = doc.getElementsByTagName ("PointOfSale"). Item (0);
NamedNodeMap attr = pointOfSale.getAttributes ();
Node nodeAttr = attr.getNamedItem ("PointOfSaleCode");
nodeAttr.setTextContent ("999");
nodeAttr = attr.getNamedItem ("ParentPointOfSale");
nodeAttr.setTextContent ("999");
pointOfSale = doc.getElementsByTagName ("PointOfSaleDescription"). item (0);
attr = pointOfSale.getAttributes ();
nodeAttr = attr.getNamedItem ("Description");
nodeAttr.setTextContent ("999");
TransformerFactory transformerFactory = TransformerFactory.newInstance ();
Transformer transformer = transformerFactory.newTransformer ();
DOMSource source = new DOMSource (doc);
StreamResult result = new StreamResult (new File (filepath));
transformer.transform (source, result);
return true;
} catch (ParserConfigurationException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
} catch (SAXException e) {
e.printStackTrace ();
} catch (TransformerConfigurationException e) {
e.printStackTrace ();
} catch (TransformerException e) {
e.printStackTrace ();
}
}
return false;
}
Answer 1, authority 100%
Instead of passing the file path
Document doc = docBuilder.parse (filepath);
Pass the file object itself
Document doc = docBuilder.parse (f);