1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package XMLParse; import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; public class domXML { //利用element方式 public static void element(NodeList list) { for (int i = 0; i < list.getLength(); i++) { Element element = (Element) list.item(i); NodeList childNode = element.getChildNodes(); for (int j = 0; j < childNode.getLength(); i++) { if (childNode.item(j).getNodeType() == Node.ELEMENT_NODE) { //获取节点 System.out.print(childNode.item(j).getNodeName() + ":"); //获取节点数值 System.out.println(childNode.item(j).getFirstChild().getNodeValue()); } } } } public static void node(NodeList list) { for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); NodeList childNodes = node.getChildNodes(); for (int j = 0; j < childNodes.getLength(); j++) { if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) { System.out.print(childNodes.item(j).getNodeName() + ": "); System.out.println(childNodes.item(j).getFirstChild().getNodeValue()); } } } } public static void main(String[] args) { //创建这个对象 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //创建下面这个对象 try { DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/XMLParse/test.xml"); NodeList list = document.getElementsByTagName("student"); node(list); } catch (Exception e) { e.printStackTrace(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
package XMLParse; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; public class saxXML { public static void main(String[] args) throws Exception { //获取sax实例对象 SAXParserFactory factory = SAXParserFactory.newInstance(); //获取SAXParse实例 SAXParser saxParser = factory.newSAXParser(); //创建handle对象 SAXDemoHandel handel = new SAXDemoHandel(); saxParser.parse("src/XMLParse/test.xml", handel); } } class SAXDemoHandel extends DefaultHandler { //遍历XML文件开始的标签 @Override public void startDocument() throws SAXException { super.startDocument(); System.out.println("sax解析开始..."); } //遍历XML文件结束标签 @Override public void endDocument() throws SAXException { super.endDocument(); System.out.println("sax解析结束..."); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes); if (qName.equals("student")) { System.out.println("===开始遍历student==="); } else if (!qName.equals("student") && !qName.equals("class")) { System.out.println("节点名称:" + qName + "----"); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(uri, localName, qName); if (qName.equals("student")) { System.out.println(qName + "遍历结束"); System.out.println("===结束遍历student==="); } } @Override public void characters(char[] ch, int start, int length) throws SAXException { super.characters(ch, start, length); String value = new String(ch, start, length).trim(); if (!value.equals("")) { System.out.println(value); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package XMLParse; import sun.plugin.dom.core.Document; import sun.plugin.dom.core.Element; import javax.print.attribute.Attribute; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.List; /** * @Author: mac * JDom解析xml * 快速开发XML应用程序。 * 是一个开源项目 * JDOM主要用来弥补DOM和SAX在实际应用当中的不足。 */ public class JDomDemo { public static void main(String[] args) throws Exception { //1.创建SAXBuilder对象 SAXBuilder saxBuilder = new SAXBuilder(); //2.创建输入流 InputStream is = new FileInputStream(new File("src/main/resources/demo.xml")); //3.将输入流加载到build中 Document document = saxBuilder.build(is); //4.获取根节点 Element rootElement = document.getRootElement(); //5.获取子节点 List<Element> children = rootElement.getChildren(); for (Element child : children) { System.out.println("通过rollno获取属性值:" + child.getAttribute("rollno")); List<Attribute> attributes = (List<Attribute>) child.getAttributes(); //打印属性 for (Attribute attr : attributes) { System.out.println(attr.getName() + ":" + attr.getValue()); } List<Element> childrenList = child.getChildren(); System.out.println("======获取子节点-start======"); for (Element o : childrenList) { System.out.println("节点名:" + o.getName() + "---" + "节点值:" + o.getValue()); } System.out.println("======获取子节点-end======"); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import java.io.File; import java.util.Iterator; import java.util.List; /** * @Author: cxx * Dom4j解析xml * @Date: 2018/5/30 12:21 */ public class Dom4JDemo { public static void main(String[] args) throws Exception { //1.创建Reader对象 SAXReader reader = new SAXReader(); //2.加载xml Document document = reader.read(new File("src/main/resources/demo.xml")); //3.获取根节点 Element rootElement = document.getRootElement(); Iterator iterator = rootElement.elementIterator(); while (iterator.hasNext()){ Element stu = (Element) iterator.next(); List<Attribute> attributes = stu.attributes(); System.out.println("======获取属性值======"); for (Attribute attribute : attributes) { System.out.println(attribute.getValue()); } System.out.println("======遍历子节点======"); Iterator iterator1 = stu.elementIterator(); while (iterator1.hasNext()){ Element stuChild = (Element) iterator1.next(); System.out.println("节点名:"+stuChild.getName()+"---节点值:"+stuChild.getStringValue()); } } } } |
from:https://blog.csdn.net/qq_42544728/article/details/125913216