旧ブログ

sambaiz.net に引っ越しました

(Java) DOMでXMLを編集

try{

DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = dbfactory.newDocumentBuilder();

Document doc = builder.parse(new BufferedInputStream(new FileInputStream("***.xml")));

 

Element element = (略)

 

要素の場合

Node aaa = element.getElementsByTagName("***”).item(*);

属性の場合

Node aaa = element.getAttributes().item(*);

 

aaa.getFirstChild().setNodeValue(書き込む値)

 

TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();

 

DOMSource source = new DOMSource(doc);
File newXML = new File("***.xml");
FileOutputStream os = new FileOutputStream(newXML);
StreamResult result = new StreamResult(os);
transformer.transform(source, result);

}catch(略)