收藏信息.xml
<?xml version="1.0" encoding="GB2312" standalone="no"?><PhoneInfo>
<Brand id="0" name="华为">
<Type name="U8650"/>
<Type name="HW123"/>
<Type name="HW321"/>
</Brand>
<Brand id="1" name="苹果">
<Type name="iPhone4"/>
</Brand>
<Brand id="2" name="三星"><type name="s120"/></Brand></PhoneInfo>
解析示例:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class ParseXml {
private Document document;
//获取DOM树
public void getDom(){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
document = builder.parse("收藏信息.xml");
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//遍历XML信息
public void showInfo(){
NodeList nodes = document.getElementsByTagName("Brand");
for(int i=0;i<nodes.getLength();i++){
Node node = nodes.item(i);
Element ele = (Element)node;
String brandName = ele.getAttribute("name");
System.out.println(brandName);
NodeList types = ele.getChildNodes();
for(int j=0;j<types.getLength();j++){
Node typeNode = types.item(j);
//如果节点是元素节点时再类型转换
if(typeNode.getNodeType() == Node.ELEMENT_NODE){
Element typeEle = (Element)typeNode;
String typeName = typeEle.getAttribute("name");
System.out.println(typeName);
}
}
}
}
//保存XML
public void saveXml(String path){
TransformerFactory factory = TransformerFactory.newInstance();
try {
//获取解析器
Transformer transformer = factory.newTransformer();
//指定特定的输出格式
transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312");
//源
DOMSource source = new DOMSource(document);
//目标
StreamResult result = new StreamResult(new FileOutputStream(path));
//解析
transformer.transform(source, result);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
//向XML添加节点
public void addEle(){
Element Brand = document.createElement("Brand");
Brand.setAttribute("name", "三星");
Element type = document.createElement("type");
type.setAttribute("name", "s120");
Brand.appendChild(type);
document.getElementsByTagName("PhoneInfo").item(0).appendChild(Brand);
saveXml("收藏信息.xml");
}
//修改XML元素,为每个Brand节点添加一个ID属性,分别为0,1,2
public void updateEle(){
NodeList brand = document.getElementsByTagName("Brand");
for(int i=0;i<brand.getLength();i++){
Node node = brand.item(i);
Element ele = (Element)node;
ele.setAttribute("id", i+"");
}
saveXml("收藏信息.xml");
}
//删除手机品牌为华为的相关元素
public void deleteEle(){
NodeList brand = document.getElementsByTagName("Brand");
for(int i=0;i<brand.getLength();i++){
Node node = brand.item(i);
Element brandEle = (Element)node;
if(brandEle.getAttribute("name").equals("华为")){
brandEle.getParentNode().removeChild(brandEle);
}
}
saveXml("收藏信息.xml");
}
public static void main(String[] args) {
ParseXml px = new ParseXml();
px.getDom();
px.showInfo();
// px.saveXml("wjb.xml");
// px.addEle();
px.updateEle();
px.deleteEle();
}
}