hi there i have an XML file wich contain some music information in a node and i would like to insert more i would like to add more node on demeand when i do i receive this error HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted. here is my XML structure
?xml version="1.0" encoding="UTF-8" standalone="no"?>
<playlist>
<trackList>
<track id="1">
<location>C:\Music\subfoder\title.mp3</location>
<creator>2Pac</creator>
<title>Young Black Male</title>
<album>2Pacalypse Now</album>
</track>
<--I WOULD LIE TO INSERT another node here track id ="2" -->
</trackList>
<--IN MANY THINGS I TESTED SOME INSERTED NODE HERE AND IT'S NOT WHAT I WANT-->
</playlist>
i use this code and i cant see what is the problem i have searcher all over internet couldn't any similar problem on forums or stack overflows.
public void addTrack(String playlistName) {
String filePath = playlistName;
File xmlFile = new File(filePath);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();//nouvelle instace de Document DocumentBuilderFactory
DocumentBuilder dBuilder;
try{dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
Node root = doc.getFirstChild();
Node subroot =root.getFirstChild();
int id = doc.getElementsByTagName("track").getLength()+1;
subroot.appendChild(createMusicElement(doc, id,playlist.get(key).getLocation(), playlist.get(key).getTrack(), playlist.get(key).getAlbum(),playlist.get(key).getArtist()));
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(doc);
StreamResult file = new StreamResult(filePath);
transformer.transform(source, file);
}catch (Exception e) {
e.printStackTrace();
}
}
thanks for your times i really apreciate!