xml 操作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了xml 操作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4312字,纯文字阅读大概需要7分钟。
内容图文
////////////////////////////////////////////////////////////////////////////
要操作的xml文件
<?xml version="1.0" encoding="GB2312" standalone="no"?><PhoneInfo>
<Brand name="华为">
<Type name="U8650"/><Type name="HW123"/>
<Type name="HW321"/>
</Brand>
<Brand name="苹果">
<Type name="iPhone4"/>
</Brand>
<Brand name="vivo">
<Type name="nb"/>
</Brand>
</PhoneInfo>
//////////////////////////////////////////////////////////////////////////////////////////////////
package xinhuiji_day15;
import java.io.File;
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.NodeList;
import org.xml.sax.SAXException;
public class xmlTest {
/**
* @param args
* @throws ParserConfigurationException
* @throws IOException
* @throws SAXException
* @throws TransformerException
*/
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerException {
// test3();
// findId("3");
addNode();
}
//查找节点的内容
public static void test3() throws ParserConfigurationException,
SAXException, IOException {
DocumentBuilderFactory daoFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = daoFactory.newDocumentBuilder();
Document doc = builder.parse("src/网易手机各地行情.xml");
NodeList nodeList = doc.getElementsByTagName("pubDate");
for(int i = 0;i<nodeList.getLength();i++){
Element e = (Element) nodeList.item(i);
System.out.println(e.getFirstChild().getNodeValue());
}
}
public void test() throws ParserConfigurationException, SAXException, IOException{
DocumentBuilderFactory daoFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = daoFactory.newDocumentBuilder();
Document doc = builder.parse("src/收藏信息.xml");
NodeList nodeList = doc.getElementsByTagName("Brand");
for(int i = 0;i<nodeList.getLength();i++){
Element e = (Element) nodeList.item(i);
// System.out.println(e.getFirstChild().getNodeValue());
System.out.println(e.getTagName());
System.out.println(e.getAttribute("name"));
NodeList nod = e.getElementsByTagName("Type");
for(int j = 0;j<nod.getLength();j++){
Element ne = (Element) nod.item(j);
System.out.println("\t"+ne.getAttribute("name"));
}
}
}
public static void findId(String id) throws ParserConfigurationException, SAXException, IOException{
DocumentBuilderFactory daoFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = daoFactory.newDocumentBuilder();
Document doc = builder.parse("src/网易手机各地行情.xml");
NodeList nodeList = doc.getElementsByTagName("item");
for(int i = 0;i<nodeList.getLength();i++){
Element e = (Element) nodeList.item(i);
String xmlId = e.getAttribute("id");
if(xmlId.equals(id)){
System.out.println(e.getElementsByTagName("title").item(0)
.getFirstChild().getNodeValue());
}
}
}
//增加节点
public static void addNode() throws ParserConfigurationException, SAXException, IOException, TransformerException{
DocumentBuilderFactory daoFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = daoFactory.newDocumentBuilder();
Document doc = builder.parse("src/收藏信息.xml");
Element brandElement = doc.createElement("Brand");
brandElement.setAttribute("name", "vivo");
Element typeElement = doc.createElement("Type");
typeElement.setAttribute("name", "nb");
Element rootElement = (Element) doc.getElementsByTagName("PhoneInfo").item(0);
brandElement.appendChild(typeElement);
rootElement.appendChild(brandElement);
//向xml文件中写
TransformerFactory tff = TransformerFactory.newInstance();
Transformer ts = tff.newTransformer();
DOMSource source = new DOMSource(doc);
ts.setOutputProperty(OutputKeys.ENCODING, "gb2312");
StreamResult result = new StreamResult(new File("src/收藏信息.xml"));
ts.transform(source, result);
}
}
原文:http://www.cnblogs.com/siashan/p/3880464.html
内容总结
以上是互联网集市为您收集整理的xml 操作全部内容,希望文章能够帮你解决xml 操作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。