标签:vax cep jdom blog tco 读取 遍历 strong children
xml文件
<?xml version="1.0" encoding="utf-8"?> <Accounts> <Account type="type1"> <code>100001</code> <pass>123</pass> <name>张三</name> <money>1000000.00</money> </Account> <Account type="type2"> <code>100002</code> <pass>123</pass> <name>李四</name> <money>1000.00</money> </Account> </Accounts>
java文件
package dom; import java.io.*; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Dom { public static void main(String arge[]) { Element element = null; // 可以使用绝对路劲 File f = new File("dom/book.xml"); // documentBuilder为抽象不能直接实例化(将XML文件转换为DOM文件) DocumentBuilder db = null; DocumentBuilderFactory dbf = null; try { // 返回documentBuilderFactory返回工厂对象 dbf = DocumentBuilderFactory.newInstance(); // 返回db对象用documentBuilderFatory对象获得返回documentBuildr对象 db = dbf.newDocumentBuilder(); // 得到一个DOM并返回给document对象 Document dt = db.parse(f); // 得到一个elment根元素 element = dt.getDocumentElement(); // 输出根元素的name System.out.println("根元素:" + element.getNodeName());//Accounts // 获得根元素下的子节点 NodeList childNodes = element.getChildNodes(); // 遍历根节点下的所有子节点 for (int i = 0; i < childNodes.getLength(); i++) {//5 #text Account #text Account #text 会把回车算成子节点,实际只有两个子节点 // 获得每个对应位置i的结点 Node node1 = childNodes.item(i); System.out.println("分别是="+node1.getNodeName()); if ("Account".equals(node1.getNodeName())) { // 如果节点的名称为"Account",则输出Account元素属性type //NamedNodeMap nnm = node1.getAttributes()的返回值是NamedNodeMap对象 //通过节点名称返回结点对象Node node = getNamedItem("type") //getNodeValue()返回的是xml节点的值 System.out.println("\r\n找到一篇账号. 所属区域: " + node1.getAttributes().getNamedItem("type") .getNodeValue() + ". "); // 获得<Accounts>下的节点 NodeList nodeDetail = node1.getChildNodes(); // 遍历<Accounts>下的节点 for (int j = 0; j < nodeDetail.getLength(); j++) {//9 #text code #text pass #text name #text money #text // 获得<Accounts>元素每一个节点 Node detail = nodeDetail.item(j); if ("code".equals(detail.getNodeName())) // 输出code System.out .println("卡号: " + detail.getTextContent()); else if ("pass".equals(detail.getNodeName())) // 输出pass System.out .println("密码: " + detail.getTextContent()); else if ("name".equals(detail.getNodeName())) // 输出name System.out .println("姓名: " + detail.getTextContent()); else if ("money".equals(detail.getNodeName())) // 输出money System.out .println("余额: " + detail.getTextContent()); } } } } catch (Exception e) { e.printStackTrace(); } } }
XML文件
<?xml version="1.0" encoding="UTF-8"?> <RESULT> <VALUE > <NO>A1234</NO> <ADDR>河南省郑州市</ADDR> </VALUE> <VALUE> <NO>B1234</NO> <ADDR>河南省郑州市二七区</ADDR> </VALUE> </RESULT>
java文件
package dom4J; import java.io.*; import java.util.*; import org.dom4j.*; import org.dom4j.io.*; public class Test { public static void main(String arge[]) { try { File f = new File("dom4J/a.xml"); SAXReader reader = new SAXReader(); //得到Document对象 Document doc = reader.read(f); //获得根节点 Element root = doc.getRootElement(); Element foo; //elementIterator从根节点遍历子节点 Iterator i = root.elementIterator("VALUE"); while(i.hasNext()){ //for (Iterator i = root.elementIterator("VALUE"); i.hasNext();) { foo = (Element) i.next(); //得到节点的内容 System.out.print("车牌号码:" + foo.elementText("NO")); System.out.println("车主地址:" + foo.elementText("ADDR")); } } catch (Exception e) { e.printStackTrace(); } } }
XML和上面相同,java代码如下
package jdom; import java.io.File; import java.util.List; import org.jdom.Document; import org.jdom.Element; import org.jdom.input.SAXBuilder; public class Test { public static void main(String arge[]) { try { SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(new File("jdom/a.xml")); Element foo = doc.getRootElement(); List allChildren = foo.getChildren(); for (int i = 0; i < allChildren.size(); i++) { System.out.print("车牌号码:" + ((Element) allChildren.get(i)).getChild("NO") .getText()); System.out.println("车主地址:" + ((Element) allChildren.get(i)).getChild("ADDR") .getText()); } } catch (Exception e) { e.printStackTrace(); } } }
注意:2,3方法需要引入相对应的jar包使用
标签:vax cep jdom blog tco 读取 遍历 strong children
原文地址:http://www.cnblogs.com/lm970585581/p/7224703.html