Dom解析XML(添加,删除,修改,保存)

Posted 【宁静的夏天】

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dom解析XML(添加,删除,修改,保存)相关的知识,希望对你有一定的参考价值。

//XML文件

<?xml version="1.0" encoding="UTF-8" ?>
<PhoneInfo>
<Brand name="华为">
<Type name="P9">

<title>标题信息</title>

</Type>
</Brand>
<Brand name="苹果">
<Type name="inphone6"/>
<Type name="inphone7">
<title>标题信息</title>

</Type>
</Brand>

</PhoneInfo>

*************************************************************************************
public class DocumentDome {
Document doc=null;
//通过工厂获得Document的对象
public void showInfo() throws ParserConfigurationException, SAXException, IOException{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.parse("src/Phone.xml");

}
//解析PhoneXML的信息
public void showInfo1(){
NodeList brandList = doc.getElementsByTagName("Brand");
for (int i = 0; i < brandList.getLength(); i++) {
Node brand = brandList.item(i);
Element element=(Element)brand;
String name = element.getAttribute("name");
NodeList typeList = element.getChildNodes();
for (int j = 0; j < typeList.getLength(); j++) {
Node type = typeList.item(j);
if (type.getNodeType()==Node.ELEMENT_NODE) {
Element typeElement=(Element)type;
String type1 = typeElement.getAttribute("name");
System.out.println();
NodeList e = doc.getElementsByTagName("title");
Element aElement=(Element) e.item(0);
String value = aElement.getFirstChild().getNodeValue();
System.out.println(name+type1+value);
}

}
}
}
//增加一个手机三星
public void addShowInfo(){
Element brandElement = doc.createElement("Brand");
brandElement.setAttribute("name", "三星");
Element typeElement = doc.createElement("Type");
typeElement.setAttribute("name", "G8");

Element titleElement = doc.createElement("title");
titleElement.setTextContent("标题信息");
typeElement.appendChild(titleElement);
brandElement.appendChild(typeElement);
doc.getElementsByTagName("PhoneInfo").item(0).appendChild(brandElement);
}

//保存文件的方法
public void saveShowInfo(String path) throws TransformerException{
TransformerFactory factory=TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
Source xmlSource=new DOMSource(doc);
Result outputTarget=new StreamResult(path);


transformer.transform(xmlSource, outputTarget);



}
//更改三星为SANXING的方法
public void changeShow(){
NodeList list = doc.getElementsByTagName("Brand");

for (int i = 0; i < list.getLength(); i++) {
Node item = list.item(i);
Element brand=(Element)item;
if (brand.getAttribute("name") .equals("三星")) {
brand.setAttribute("name", "SANXING");
}

}

}

//删除的方法SANXING
public void deleShow(){
NodeList list = doc.getElementsByTagName("Brand");

for (int i = 0; i < list.getLength(); i++) {
Node item = list.item(i);
Element brand=(Element)item;
if (brand.getAttribute("name") .equals("SANXING")) {
//通过他的父节点删除
brand.getParentNode().removeChild( brand) ;
}

}



}

/**
*
*
* 用Document方法解析、添加、删除、保存文件 XML
* @param args
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
* @throws TransformerException
*/
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerException {
DocumentDome d=new DocumentDome();
d.showInfo();
d.showInfo1();
//d.addShowInfo();
d.changeShow();
d.deleShow();
d.saveShowInfo("src/Phone.xml");
}

}

以上是关于Dom解析XML(添加,删除,修改,保存)的主要内容,如果未能解决你的问题,请参考以下文章

dom解析xml文档的步骤

Android之DOM解析XML

Android之DOM解析XML

dom4j的介绍

XML

详解SimpleXML添加_修改_删除_遍历XML节点属性