このトリックを行うJavaアプリケーションを作成できます。
たとえば、必要なデータを読み取るには:
File fXmlFile = new File(fileName);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList playlists = doc.getElementsByTagName("playlist"); // Get all Nodes named 'playlist'
if(playlists != null && playlists.getLength() > 0){
for (int temp = 0; temp < playlists.getLength(); temp++) { // Iterate through all 'playlists'
if(playlists.item(temp).getNodeType() == Node.ELEMENT_NODE){ //Check if they are Elements (just to be sure)
Element playlist = (Element) playlists.item(temp); // Cast to Element
NodeList items = playlist.getElementsByTagName("item"); // Get Nodes named 'item' in 'playlist'
for (int temp1 = 0; temp1 < items.getLength(); temp1++) { // Iterate through all items
if(items.item(temp1).getNodeType() == Node.ELEMENT_NODE){
Element item = (Element) items.item(temp1);
NodeList imgs = item.getElementsByTagName("image");
Node img = imgs.item(0); // We suppose there is only one image-node in the item-node
if(img.getNodeType() == Node.ELEMENT_NODE){
Element image = (Element) img;
String imgUrl = image.getTextContent(); // Get the textContent of image --> Store it in an Array(List) etc...
}
}
}
}
}
}
もちろん、他のノードについても同じことをしなければなりません。
そして、DocumentBuilderを使用してXMLファイルに書き戻します。
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("playlist");
doc.appendChild(rootElement);
Element item = doc.createElement("item");
rootElement.appendChild(item);
Element image = doc.createElement("image");
item.appendChild(image);
image.appendChild(doc.createTextNode("HERE YOUR ImgURL "));
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(fileName));
transformer.transform(source, result);
これが少し役立つことを願っています。