当前位置: 首页 > 工具软件 > JAXP > 使用案例 >

java jaxp_java解析xml数据(使用jaxp的方式)

邓昊天
2023-12-01

packageng.www.jaxp;importjava.io.IOException;importjavax.xml.parsers.DocumentBuilder;importjavax.xml.parsers.DocumentBuilderFactory;importjavax.xml.parsers.ParserConfigurationException;importjavax.xml.transform.Transformer;importjavax.xml.transform.TransformerConfigurationException;importjavax.xml.transform.TransformerException;importjavax.xml.transform.TransformerFactory;importjavax.xml.transform.dom.DOMSource;importjavax.xml.transform.stream.StreamResult;importorg.w3c.dom.Document;importorg.w3c.dom.Element;importorg.w3c.dom.Node;importorg.w3c.dom.NodeList;importorg.w3c.dom.Text;importorg.xml.sax.SAXException;/*** 实现jaxp操作xml

*@authorWangWeiWei

**/

public classTestJaxp {/***@paramargs

*@throwsParserConfigurationException

*@throwsIOException

*@throwsSAXException

*@throwsTransformerException*/

public static void main(String[] args) throwsParserConfigurationException, SAXException, IOException, TransformerException {//addNode();//editNode();//deleteNode();//selectSin();

transervalNode();//selectAll();

}private static void transervalNode() throwsParserConfigurationException, SAXException, IOException {/** 此处是遍历xml的节点*/DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();

DocumentBuilder builder=builderFactory.newDocumentBuilder();

Document document=builder.parse("src/person.xml");//编写一个方法实现遍历操作

list1(document);

}private static voidlist1(Node node) {if(node.getNodeType()==Node.ELEMENT_NODE){

System.out.println(node.getNodeName());

}//得到一层子节点

NodeList nodelist=node.getChildNodes();for(int i=0;i

Node node1=nodelist.item(i);

list1(node1);

}

}private static void deleteNode() throwsParserConfigurationException, SAXException, IOException, TransformerException {/** 此处是删除xml的节点*/DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();

DocumentBuilder builder=builderFactory.newDocumentBuilder();

Document document=builder.parse("src/person.xml");//获取sex元素

Node node=document.getElementsByTagName("sex").item(0);

Node parentNode=node.getParentNode();

parentNode.removeChild(node);//回写到本地xml文件中

TransformerFactory factory=TransformerFactory.newInstance();

Transformer former=factory.newTransformer();

former.transform(new DOMSource(document), new StreamResult("src/person.xml"));

}private static void editNode() throwsParserConfigurationException, SAXException, IOException, TransformerException {/** 编辑xml数据(修改sex下的文本数据为nan)

* 使用sexTextContent方法*/DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();

DocumentBuilder builder=builderFactory.newDocumentBuilder();

Document document=builder.parse("src/person.xml");//获取sex的节点

Node sexNode = document.getElementsByTagName("sex").item(0);

sexNode.setTextContent("nan");//内存中已经修改完毕,现在需要将内存中的数据回写到本地xml文件中才会生效

TransformerFactory factory=TransformerFactory.newInstance();

Transformer former=factory.newTransformer();

former.transform(new DOMSource(document), new StreamResult("src/person.xml"));

}private static void addNode() throwsParserConfigurationException, SAXException, IOException, TransformerException{/** 这里操作需要会写xml,要不然只是在内存中修改了xml数据,本地文件不会有改变*/DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();

DocumentBuilder builder=builderFactory.newDocumentBuilder();

Document document=builder.parse("src/person.xml");

Node node=document.getElementsByTagName("p1").item(0);

Element element=document.createElement("sex");

Text text= document.createTextNode("nv");

element.appendChild(text);

node.appendChild(element);//会写xml数据到本地中

TransformerFactory formerFactory=TransformerFactory.newInstance();

Transformer former=formerFactory.newTransformer();

former.transform(new DOMSource(document), new StreamResult("src/person.xml"));

}private static void selectSin() throwsParserConfigurationException, SAXException, IOException{/** 1.创建解析器工厂

* 2.根据解析器工厂创建解析器

* 3.解析xml,返回document

* 4.得到所有的name元素

* 5.利用返回的集合里面的item(0)来获得第一个name元素

* 6.使用getTextContext方法来获得具体的值*/

//创建解析器工厂

DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();//根据解析器工厂创建一个解析器

DocumentBuilder builder=builderFactory.newDocumentBuilder();//根据解析器来返回xml的document对象

Document document=builder.parse("src/person.xml");//根据document对象来获取所有的name元素,然后利用item(0)获取第一个元素

Node node=document.getElementsByTagName("name").item(1);

String s=node.getTextContent();

System.out.println(s);

}private static void selectAll() throwsParserConfigurationException,

SAXException, IOException {//查询xml中所有标签为name的值

/** 1.创建解析器工厂

* 2.根据解析器工厂创建解析器

* 3.解析xml返回document

* 4.得到所有的name元素

* 5.遍历集合,得到每一个name元素*/

//创建解析器工厂

DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();//创建解析器

DocumentBuilder builder=builderFactory.newDocumentBuilder();//解析xml返回document

Document document= builder.parse("src/person.xml");

NodeList list= document.getElementsByTagName("sex");//遍历开始

for(int i=0;i

Node name1=list.item(i);//得到那么里面元素的值

String nametext=name1.getTextContent();

System.out.println(nametext);

}

}

}

 类似资料: