日期:2014-05-20 浏览次数:21090 次
package my.test; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class uDom { Document doc=null; public uDom(String xmlfile) { DocumentBuilder dbf; try { dbf = DocumentBuilderFactory.newInstance().newDocumentBuilder(); doc = dbf.parse(xmlfile); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 读取XML文件节点内容 * @param nodeName 节点名称 * @return */ public Object[] readNode(String nodeName) { Object nodeValue[]=null; NodeList nodelist = doc.getElementsByTagName(nodeName); nodeValue=new Object[nodelist.getLength()]; //System.out.println("getLength:" + nodelist.getLength()); //System.out.println("getNodeName"+"\t"+"getNodeValue"+"\t"+"getAttribute"); for (int i = 0; i < nodelist.getLength(); i++) { Element element =(Element)nodelist.item(i); //System.out.println(element.getNodeName()+"\t"+element.getNodeValue()+"\t"+element.getAttribute("num")); nodeValue[i]=element.getNodeValue(); } return nodeValue; } /** * 读取XML文件节点属性内容 * @param nodeName 节点名称 * @param Attribute 节点属性名称 * @return */ public Object[] readNode(String nodeName,String Attribute) { Object AttrValue[]=null; NodeList nodelist = doc.getElementsByTagName(nodeName); AttrValue=new Object[nodelist.getLength()]; for (int i = 0; i < nodelist.getLength(); i++) { Element element =(Element)nodelist.item(i); //System.out.println(element.getNodeName()+"\t"+element.getNodeValue()+"\t"+element.getAttribute("num")); AttrValue[i]=element.getAttribute(Attribute); } return AttrValue; } public uDom() { // TODO Auto-generated constructor stub try { this.readxml(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void readxml() throws ParserConfigurationException, SAXException, IOException { String nodename = null; short nodetype = 0; String nodevalue = null; DocumentBuilder dbf = DocumentBuilderFactory.newInstance().newDocumentBuilder(); System.out.println("Get DocumentBuilder Successful..."); Document doc = dbf.parse("d:/TestFile/links.xml"); System.out.println("Get Document Successful..."); NodeList nl1 = doc.getElementsByTagName("message"); System.out.println("Get NodeList(links) Successful..."); System.out.println("getLength:" + nl1.getLength()); Node node2 = doc.getFirstChild().getChildNodes().item(1); // Node node3 = doc.getFirstChild(); Node node1 = nl1.item(4); // Node node2 = doc.getFirstChild(); // Node node3 = node2.getFirstChild(); // Node node4 = node3.getFirstChild(); for (int i = 0; i < 2; i++) { System.out.println(node2.getChildNodes().item(i).getNodeName()); System.out.println(node2.getChildNodes().item(i).getNodeValue()); } // System.out.println(node2.getFirstChild().getNextSibling().getNodeName()); System.exit(0); nodename = node1.getFirstChild().getNodeName(); nodetype = node1.getFirstChild().getNodeType(); nodevalue = node1.getFirstChild().getNodeValue(); System.out.println("getNodeName:" + nodename); System.out.println("getNodeType:" + nodetype); System.out.println("getNodeValue:" + nodevalue); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub uDom udom = new uDom("xml/test.XML"); System.out.println("读取XML文件成功~~"); Object[] temp=udom.readNode("root","num"); System.out.println("获得节点长度为:"+temp.length); for(int i=0;i<temp.length;i++) { //temp[i]可能会得到空指针异常 System.out.println(i+" Value: "+temp[i].toString()); } } }