Skip to content
Advertisement

Edit an xml file element using dom

Please tell me what I’m doing wrong. I work with javafx and by clicking on the button, when the listview element is selected, I pass an object of the Country class to the editing method, but the xml file is not being edited, what is the problem? I attached an xml file, a broken edit function and a working delete function. I think it’s about the setContext I’m calling? But I’m not sure

Xml file:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<CountrysList lastId="3">
<Country id="1">
    <name>Russia</name>
    <continent>Eurasia</continent>
    <area>17125191</area>
    <population>145557576</population>
    <capital>Moscow</capital>
</Country>
<Country id="2">
    <name>Russia</name>
    <continent>Eurasia</continent>
    <area>17125191</area>
    <population>145557576</population>
    <capital>Moscow</capital>
</Country>
<Country id="3">
    <name>Russia</name>
    <continent>Eurasia</continent>
    <area>17125191</area>
    <population>145557576</population>
    <capital>Moscow</capital>
</Country>

Element search function:

    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;
}

Broken editing function:

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));
    }
}

Working deletion function:

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));
    }
}

Advertisement

Answer

I found the mistake.Everything is really like that, I forgot to create a new instance of the class with the changed fields and passed it with the previous ones, so everything worked

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement