首页 > 代码库 > xml 操作
xml 操作
////////////////////////////////////////////////////////////////////////////
要操作的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);
}
}