浏览 2467 次
锁定老帖子 主题:java dom解析XML
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-11-05
有人要java dom解析的XML通讯录 洒家给写了个 用swing做的 解析XML那部分代码:
package com.test.parseXML; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; 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 java.awt.List; import java.io.*; import java.util.ArrayList; import java.util.Vector; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Text; import org.xml.sax.SAXException; public class ParseXML { private Document getDocument(String filename) { DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); factory.setIgnoringElementContentWhitespace(true); try { DocumentBuilder domParser=factory.newDocumentBuilder(); try { Document document=domParser.parse(new File(filename)); return document; } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } catch (ParserConfigurationException e) { e.printStackTrace(); } return null; } public ArrayList<ArrayList> getNodeInfo(String filename) { ArrayList<ArrayList> myli=new ArrayList<ArrayList>(); Document document=getDocument(filename); NodeList nodelist=document.getElementsByTagName("anpai"); for(int i=0;i<nodelist.getLength();i++) { String date=document.getElementsByTagName("date").item(i).getTextContent(); String time=document.getElementsByTagName("time").item(i).getTextContent(); String activity=document.getElementsByTagName("activity").item(i).getTextContent(); ArrayList<String> li=new ArrayList<String>(); li.add(date); li.add(time); li.add(activity); myli.add(li); } return myli; } public ArrayList<ArrayList> getQueryNodeInfo(String filename,String query_date) { ArrayList<ArrayList> myli=new ArrayList<ArrayList>(); Document document=getDocument(filename); NodeList nodelist=document.getElementsByTagName("anpai"); for(int i=0;i<nodelist.getLength();i++) { String date=document.getElementsByTagName("date").item(i).getTextContent(); if(date.equals(query_date)) { String time=document.getElementsByTagName("time").item(i).getTextContent(); String activity=document.getElementsByTagName("activity").item(i).getTextContent(); ArrayList<String> li=new ArrayList<String>(); li.add(date); li.add(time); li.add(activity); myli.add(li); } } return myli; } public void deleteNode(String filename,int rowCount) { Document document=getDocument(filename); NodeList nodelist=document.getElementsByTagName("anpai"); Node anpai_node=nodelist.item(rowCount);//anpai节点 anpai_node.getParentNode().removeChild(anpai_node); updateXML(filename,document); } public void addNode(String filename,String date_str,String time_str,String activity_str) { Document document=getDocument(filename); Node node=document.createElement("anpai"); Node node_child1=document.createElement("date"); Node node_child2=document.createElement("time"); Node node_child3=document.createElement("activity"); Text textnode_date=document.createTextNode(date_str); Text textnode_time=document.createTextNode(time_str); Text textnode_activity=document.createTextNode(activity_str); node_child1.appendChild(textnode_date); node_child2.appendChild(textnode_time); node_child3.appendChild(textnode_activity); node.appendChild(node_child1); node.appendChild(node_child2); node.appendChild(node_child3); document.getDocumentElement().appendChild(node); updateXML("src/schedule.xml",document); } public String[] getNodeInfoByRow(String filename,int rowcount) { Document document=getDocument(filename); String date=document.getElementsByTagName("date").item(rowcount).getTextContent(); String time=document.getElementsByTagName("time").item(rowcount).getTextContent(); String activity=document.getElementsByTagName("activity").item(rowcount).getTextContent(); String []arr={date,time,activity}; return arr; } public void editNode(String filename,int whichnode,String e_date,String e_time,String e_activity) { Document document=getDocument(filename); //System.out.println(document.getElementsByTagName("date").item(whichnode).getChildNodes().item(0).getNodeValue()); document.getElementsByTagName("date").item(whichnode).getChildNodes().item(0).setNodeValue(e_date); document.getElementsByTagName("time").item(whichnode).getChildNodes().item(0).setNodeValue(e_time); document.getElementsByTagName("activity").item(whichnode).getChildNodes().item(0).setNodeValue(e_activity); updateXML(filename,document); } public void updateXML(String filename,Document document) { TransformerFactory tfactory=TransformerFactory.newInstance(); try { Transformer transformer=tfactory.newTransformer(); DOMSource domsource=new DOMSource(document); StreamResult result = new StreamResult(new File(filename)); try { transformer.transform(domsource,result); } catch (TransformerException e) { e.printStackTrace(); } } catch (TransformerConfigurationException e) { e.printStackTrace(); } } } 继承AbstractTableModel: package com.test.model; import java.util.ArrayList; import java.util.Vector; import javax.swing.table.AbstractTableModel; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import com.test.parseXML.ParseXML; public class ScheModel extends AbstractTableModel{ private Vector columnNames; private Vector rowData; public ScheModel() { init(""); } public ScheModel(String date) { init(date); } public void init(String date) { rowData=new Vector(); columnNames=new Vector(); columnNames.add("日期"); columnNames.add("时间"); columnNames.add("事件"); if(date=="") { ParseXML parse=new ParseXML(); ArrayList<ArrayList> al=parse.getNodeInfo("src/schedule.xml"); for(int i=0;i<al.size();i++) { ArrayList<String> a=al.get(i); Vector hang=new Vector(); for(int j=0;j<a.size();j++) { hang.add(a.get(j)); } rowData.add(hang); } } else { ParseXML parse=new ParseXML(); ArrayList<ArrayList> al=parse.getQueryNodeInfo("src/schedule.xml",date); for(int i=0;i<al.size();i++) { ArrayList<String> a=al.get(i); Vector hang=new Vector(); for(int j=0;j<a.size();j++) { hang.add(a.get(j)); } rowData.add(hang); } } } @Override public int getColumnCount() { return columnNames.size(); } @Override public int getRowCount() { return rowData.size(); } @Override public Object getValueAt(int arg0, int arg1) { return ((Vector) rowData.get(arg0)).get(arg1); } @Override public String getColumnName(int arg0) { return (String) columnNames.get(arg0); } } OK 剩下的代码就不上了
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2011-11-06
用Castor和JavaBean,你这些代码都可以省略掉
|
|
返回顶楼 | |