标签:size text length getattr tst print elements 子节点 leo
package lianxi;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
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;
public class Demo {
private static Document docu;
public static void main(String[] args) throws Exception {
read();
//add();
delete();
show();
save();
}
private static void read() throws Exception {
//创建DocumentBuilderFactor,工厂
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
//2.解析器工厂对象创建解析器对象
DocumentBuilder builder=factory.newDocumentBuilder();
//添加
docu =builder.parse("src/na.xml");
}
private static void show(){
//遍历节点
NodeList brad=docu.getElementsByTagName("学生");
for(int i=0;i<brad.getLength();i++){
Node nod=(Node)brad.item(i);
//把节点转化为元素节点
Element ele=(Element)nod;
String str=ele.getTagName();
NodeList types=ele.getChildNodes();//查询下面的子节点
for(int j=0;j<types.getLength();j++){
Node node=(Node)types.item(j);
if(node.getNodeType()==Node.ELEMENT_NODE){
Element elemen=(Element)node;
String str1=elemen.getTextContent();
System.out.println(elemen.getNodeName()+str1);
}
}
}
}
private static void save() throws Exception{
//保存xml借助转化器
TransformerFactory factory=TransformerFactory.newInstance();
//indent-number表示缩进的位置
factory.setAttribute("indent-number", 4);
//转换器得到对象
Transformer former=factory.newTransformer();
//这里设置一下Outputkeys的关键字
former.setOutputProperty(OutputKeys.INDENT,"YES");
DOMSource source=new DOMSource(docu);//这个需要传入一个xml文件
//new OutputStreamWriter(out,utf-8);
StreamResult result=new StreamResult(new OutputStreamWriter(new FileOutputStream("src/na.xml"),"utf-8"));
//调用其中一个方法transform(source,result)
former.transform(source, result);
}
private static void add() throws Exception{
Element element=docu.createElement("学生");
//可以使用getAttribute来添加一个属性
element.setAttribute("年级","大四");
docu.getElementsByTagName("班级").item(0).appendChild(element);
save();
}
private static void delete() throws Exception{
NodeList brands=docu.getElementsByTagName("学生");//找到标志
//for(int i=0;i<brands.getLength();i++){
Node brand=brands.item(1);
//节点转化为元素节点
Element ele=(Element)brand;
//if(ele.getAttribute("name").equals("小米9")){
//它自己没有权力删除自己,所有先找到父节点,通过父节点来删除
ele.getParentNode().removeChild(ele);
//}
// }
//save();
}
}
package cn.xml;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
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;
public class Xmlfactor {
private Document dd=null;
public static void main(String[] args) {
Xmlfactor ss=new Xmlfactor();
try {
ss.GetDocument();
//ss.add();
ss.delete();
ss.show();
//ss.update();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void GetDocument() throws Exception{
DocumentBuilderFactory doc= DocumentBuilderFactory.newInstance();
DocumentBuilder builder=doc.newDocumentBuilder();
dd=builder.parse("First.xml");
}
private void show(){
//获取节点集合 getElementsByTagname
NodeList brand=dd.getElementsByTagName("size");
for(int i=0;i<brand.getLength();i++){
Node node=brand.item(i);//类似于数组,使用item遍历
// System.out.println(node);
//从节点准换为具体的元素点
Element elebrand=(Element)node;
String attr=elebrand.getAttribute("aa");//遍历元素点的内容
NodeList types=elebrand.getChildNodes();
System.out.println(attr);
//接下来要查找brand元素节点里的子节点: getChildNodes():
for(int j=0;j<types.getLength();j++){
Node typenode=types.item(j);
//判断是元素节点,因为可能是其他节点
if(typenode.getNodeType()==Node.ELEMENT_NODE){
//把节点转化为元素节点
Element eleType=(Element)typenode;
String type=eleType.getAttribute("name");
System.out.println(attr+type);
}
}
}
}
private void save() throws Exception{
//保存xml,需要借助转换器
TransformerFactory factor=TransformerFactory.newInstance();
//indent-number表示缩进的位置
factor.setAttribute("indent-number", 4);
//转换器工工厂得到准换器对象
Transformer tran=factor.newTransformer();
//这里设置一下Outputkeys的关键字
tran.setOutputProperty(OutputKeys.INDENT, "YES");
//调用其中一个方法transform(source,result)
DOMSource souce=new DOMSource(dd);
//new OutputStreamWriter(out,utf-8);
StreamResult result=new StreamResult(new OutputStreamWriter(new FileOutputStream("First.xml"),"utf-8"));
tran.transform(souce, result);
}
private void add() throws Exception{
//创建一个元素集createElement
Element element=dd.createElement("size");
//给brand设置一个属性 setAttribute
element.setAttribute("aa", "asdasd");
//创建一个元素<Size tt="sdf">
//将eleme作为Element的子元素
Element ele1=dd.createElement("Type");
ele1.setAttribute("name", "小米9");
element.appendChild(ele1);
//将新建的元素放进根目录中
dd.getElementsByTagName("cloth").item(1).appendChild(element);
save();
}
//添加一个属性id
private void update() throws Exception{
//首先获取指定标志
NodeList brands=dd.getElementsByTagName("size");
//遍历
for(int i=0;i<brands.getLength();i++){
//得到每一个元素点
Node brand=brands.item(i);
//把节点转化为元素节点
Element ele=(Element)brand;
ele.setAttribute("id",i+"");
}
save();
}
private void delete() throws Exception{
NodeList brands=dd.getElementsByTagName("Type");//找到标志
for(int i=0;i<brands.getLength();i++){
Node brand=brands.item(i);
//节点转化为元素节点
Element ele=(Element)brand;
if(ele.getAttribute("name").equals("小米9")){
//它自己没有权力删除自己,所有先找到父节点,通过父节点来删除
ele.getParentNode().removeChild(ele);
}
}
save();
}
}
标签:size text length getattr tst print elements 子节点 leo
原文地址:https://www.cnblogs.com/liuchunyong/p/10430104.html