day05总结
主要学习DOM技术以及JDOM/DOM4J技术
DOM技术把Xml文件在内存中构造树状结构,可以便利和修改节点
public class DomUtils {
public static void main(String[] args) throws Exception {
DomUtils domUtils=new DomUtils();
//静态方法不能直接调用非静态的方法,需要实例化对象
domUtils.getDoc("src/PhoneInfo.xml");
//domUtils.add();
//domUtils.delete();
domUtils.addId();
domUtils.save("src/PhoneInfo.xml");
domUtils.showInfo();
}
//首先把xml转换为dom对象
private Document doc;
//得到doc对象
public void getDoc(String path){
try {
//得到工厂
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
//通过doc工厂得到解析器
DocumentBuilder db=dbf.newDocumentBuilder();
doc= db.parse(new File(path));
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//展示xml里的数据
public void showInfo(){
NodeList nodeList=doc.getElementsByTagName("Brand");
for(int i=0;i<nodeList.getLength();i++){
Node nodeBrand= nodeList.item(i);
//把node节点转化为属性节点
Element elementBrand=(Element)nodeBrand;
String name=elementBrand.getAttribute("name");
System.out.println(name);
NodeList typeList= elementBrand.getChildNodes();
for(int j=0;j<typeList.getLength();j++){
Node typeNode=typeList.item(j);
if(typeNode.getNodeType()==Node.ELEMENT_NODE){
Element elementType=(Element) typeNode;
String typeName=elementType.getAttribute("name");
System.out.println(typeName);
}
}
}
}
//增加一个节点
public void add(){
//首先要创建一个节点
Element elementBrand=doc.createElement("Brand");
//给其属性来赋值
elementBrand.setAttribute("name", "oppo");
//创建一个type节点
Element elementType=doc.createElement("Type");
elementType.setAttribute("name","oppoR17");
//添加父子关系
elementBrand.appendChild(elementType);
//添加Brand与父节点的
doc.getElementsByTagName("PhoneInfo").item(0).appendChild(elementBrand);
}
//保存
public void save(String path) throws Exception{
//得到写入的工厂dom到文件里的工厂
//得到转换器的工厂
TransformerFactory tff =TransformerFactory.newInstance();
Transformer tf= tff.newTransformer();
DOMSource dSource=new DOMSource(doc);
StreamResult sr=new StreamResult(new OutputStreamWriter(
new FileOutputStream(path),"utf-8"));
tf.transform(dSource, sr);
}
public void delete(){
NodeList nodeList =doc.getElementsByTagName("Brand");
for(int i=nodeList.getLength();i>=0;i--){
Node nodeBrand= nodeList.item(i);
Element elementBrand=(Element)nodeBrand;
if(elementBrand.getAttribute("name").equals("oppo")){
//根据父节点来删除子节点
elementBrand.getParentNode().removeChild(elementBrand);
}
}
}
public void addId(){
NodeList nodeList =doc.getElementsByTagName("Brand");
for(int i=0;i<nodeList.getLength();i++){
Node nodeBrand= nodeList.item(i);
Element elementBrand=(Element)nodeBrand;
elementBrand.setAttribute("id",i+1+"");
}
}
}
Dom4技术,
public class ConfigUtils {
private Document doc;
public static void main(String[] args) {
ConfigUtils configUtils=new ConfigUtils();
configUtils.show("src/PhoneInfo.xml");
//configUtils.add();
//configUtils.delete();
configUtils.update();
configUtils.save("src/PhoneInfo.xml");
configUtils.showInfo();
}
//获取dom对象
public void show(String path){
try {
SAXReader reder=new SAXReader();
doc=reder.read(new File(path));
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//展示所有的信息
public void showInfo(){
//获取根节点getRootElement()
Element element=doc.getRootElement();
Iterator iterator= element.elementIterator();
while(iterator.hasNext()){
Element element2=(Element) iterator.next();
System.out.println(element2.attributeValue("name"));
//得到子元素的迭代器
Iterator it2=element2.elementIterator();
while(it2.hasNext()){
Element e= (Element) it2.next();
System.out.println(e.attributeValue("name"));
}
}
}
public void save(String path){//把所有的输入的数据格式化成utf-8的编码格式
try {
FileWriter fileWriter=new FileWriter(path);
//格式化 编码格式
OutputFormat outputFormat =OutputFormat.createPrettyPrint();
outputFormat.setEncoding("gbk");
XMLWriter writer =new XMLWriter(fileWriter, outputFormat);
writer.write(doc);
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void add(){
//得到根节点
Element elementRoot=doc.getRootElement();
//创建Brand节点
Element elementBrand=elementRoot.addElement("Brand");
//设置属性
elementBrand.addAttribute("name","oppo");
//创建Type节点
Element elementType=elementBrand.addElement("Type");
//设置属性
elementType.addAttribute("name", "r17");
}
//删除
public void delete(){
//获取根节点getRootElement()
Element elementRoot=doc.getRootElement();
Iterator iterator= elementRoot.elementIterator();
while(iterator.hasNext()){
Element elementBrand=(Element) iterator.next();
if(elementBrand.attributeValue("name").equals("oppo")){
elementBrand.getParent().remove(elementBrand);
}
}
}
//更改,加Id
public void update(){
//获取根节点getRootElement()
Element elementRoot=doc.getRootElement();
Iterator iterator= elementRoot.elementIterator();
int i=1;
while(iterator.hasNext()){
Element elementBrand=(Element) iterator.next();
elementBrand.addAttribute("id", (i++)+"");
}
}
}