Я работаю с Javafx и нажимая на кнопку, когда выбран элемент ListView, я передаю объект класса страны методу редактирования, но файл XML не отредактируется. В чем проблема? Я думаю, что речь идет о SetContext, который я звоню, но я не уверен.
Russia
Eurasia
17125191
145557576
Moscow
Russia
Eurasia
17125191
145557576
Moscow
Russia
Eurasia
17125191
145557576
Moscow
< /code>
Функция поиска элемента: < /p>
public Element findCountry(Document document, int id) {
NodeList countries = document.getElementsByTagName("Country");
Element currentCountry = null;
int i = 0;
while(i < countries.getLength() && currentCountry == null){
Node node = countries.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE){
Element element = (Element) node;
if(String.valueOf(id).equals(element.getAttribute("id"))){
currentCountry = element;
}
}
i++;
}
return currentCountry;
}
< /code>
Функция сломанного редактирования: < /p>
public void redact(Country country) throws ParserConfigurationException, IOException, SAXException, TransformerException {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document document = docBuilder.parse(path);
Element currentCountry = findCountry(document, country.getId());
if(currentCountry != null){
NodeList children = currentCountry.getChildNodes();
for (int j=0; j < children.getLength(); j++){
Node node = children.item(j);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
switch (element.getTagName()){
case "name":
element.setTextContent(country.getName());
break;
case "continent":
element.setTextContent(country.getContinent());
break;
case "area":
element.setTextContent(String.valueOf(country.getArea()));
break;
case "population":
element.setTextContent(String.valueOf(country.getPopulation()));
break;
case "capital":
element.setTextContent(country.getCapital());
break;
}
}
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(new DOMSource(document), new StreamResult(path));
}
}
< /code>
Функция рабочей удаления: < /p>
public void delete(Country country) throws ParserConfigurationException, IOException, SAXException, TransformerException {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document document = docBuilder.parse(path);
Element rootElement = document.getDocumentElement();
Element currentCountry = findCountry(document, country.getId());
if(currentCountry != null){
rootElement.removeChild(currentCountry);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(new DOMSource(document), new StreamResult(path));
}
}
Подробнее здесь: https://stackoverflow.com/questions/734 ... -using-dom
Отредактируйте элемент файла XML, используя DOM [закрыто] ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение