Good day to all :) In general, this is the question: I created a class with the method of saving data that is in the HashMap, in an XML file. The map is not exactly empty, it does not pop up any errors, the xml file is created, but it is empty :( show Krivoruk where it is messed up? I doubt the addition of elements in the for loop. The source file should look like this:

<employees> <staff depcode="1" depjob="clean"> <description>bla-bla-bla</description> </staff> <staff depcode="2" depjob="walk"> <description>ha-ha-ha</description> </staff> <staff depcode="3" depjob="smile"> <description>=):)=P</description> </staff> </employees> 

And here is the class itself:

 package db_xml; import org.w3c.dom.Document; import org.w3c.dom.Element; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.Map; public class Saver { private String fileName; public Saver(String fileName){ this.fileName=fileName; } public void writeToFile(Map<Key,String> hashMap) throws ParserConfigurationException, TransformerException, FileNotFoundException { DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.newDocument(); Element employees=document.createElement("employees"); for (Map.Entry<Key, String> entry : hashMap.entrySet()) { Element staff=document.createElement("staff"); employees.appendChild(staff); staff.setAttribute("depcode", entry.getKey().getDepCode()); staff.setAttribute("depjob", entry.getKey().getDepJob()); Element description=document.createElement("description"); staff.appendChild(description); description.setTextContent(entry.getValue()); } Transformer transformer=TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT,"yes"); transformer.transform(new DOMSource(document), new StreamResult(new FileOutputStream(this.fileName))); } } 

    1 answer 1

    Problem solved) I forgot to make employees a descendant for document)