java学习是xml解析

简单实现xml解析的四种方法:

方法一:

package Ray.xml;

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 TestXML2 {
	public static void main(String[] args) {
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder document = documentBuilderFactory.newDocumentBuilder();
			Document parse = document.parse("hello.xml");
			NodeList element = parse.getElementsByTagName("student");
			for(int i = 0;i<element.getLength();i++){
				//System.out.println(element.item(i));
				Node item = element.item(i);
				Element e = (Element)item;
				String attribute = e.getAttribute("id");
				System.out.println(attribute);
				Element name = (Element) e.getElementsByTagName("name").item(0);
				Element gender =(Element) e.getElementsByTagName("gender").item(0);
				Element score =(Element) e.getElementsByTagName("score").item(0);
				Element course =(Element) e.getElementsByTagName("course").item(0);
				System.out.println(name.getTagName()+"-------"+name.getTextContent());
				System.out.println(gender.getTagName()+"------"+gender.getTextContent());
				System.out.println(score.getTagName()+"-----"+score.getTextContent());
				System.out.println(course.getTagName()+"-------"+course.getTextContent());
				System.out.println("---------------------");
			}
			
		} 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();
		}
	}
}
方法二:使用递归
package Ray.xml;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class TestXML3 {
	public static void main(String[] args) {
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder dBuilder = documentBuilderFactory.newDocumentBuilder();
			Document parse = dBuilder.parse("hello.xml");
			Element document= parse.getDocumentElement();
			
			parseXml(document);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void parseXml(Element ele) {
		System.out.print("<");
		System.out.print(ele.getTagName());
		if(ele.hasAttributes()){
			NamedNodeMap attrs = ele.getAttributes();
			for(int i = 0;i<attrs.getLength();i++){
				Attr attr = (Attr) attrs.item(i);
				System.out.print(attr+" ");
			}
		}
		System.out.print(">");
		NodeList childNodes = ele.getChildNodes();
		for(int i=0;i<childNodes.getLength();i++){
			Node item = childNodes.item(i);
			if(item.getNodeType()==item.ELEMENT_NODE){
				parseXml((Element)item);
			}else{
				System.out.print(item.getTextContent());
			}
		}
		System.out.print("</"+ele.getTagName()+">");
	}
}
方法三:使用dom4j包
package Ray.xml;


import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class TestXml4 {
	public static void main(String[] args) {
		SAXReader saxReader =new SAXReader();
		try {
			Document document = saxReader.read("hello.xml");
			Element ele = document.getRootElement();
			
			
			List elements = ele.elements();
			
			/*for(int i =0;i<elements.size();i++){
				Element stu = (Element)elements.get(i);
				Attribute attribute = stu.attribute("id");
				System.out.println(attribute.getName()+"-----"+attribute.getValue());
			}*/
			
			
			Iterator elIterator = ele.elementIterator();
			while(elIterator.hasNext()){
				
				Element next = (Element) elIterator.next();
				
				Attribute attribute = next.attribute("id");
				System.out.println(attribute.getName()+"-"+attribute.getValue());
				
				List<Element> element = next.elements();
				
				for(Element list:element){
					System.out.println(list.getName()+"----"+list.getStringValue());
				}
			}
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}




package Ray.xml;

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.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
 * 
 * @author Marie
 *
 */
public class TestXML {
	public static void main(String[] args)  {
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder;
		try {
			dBuilder = documentBuilderFactory.newDocumentBuilder();
			Document parse = dBuilder.parse("hello.xml");
			NodeList childNodes = parse.getChildNodes();
			Node scores = childNodes.item(2);
			
			NodeList childNodes2 = scores.getChildNodes();
			for(int i =0;i<childNodes2.getLength();i++){
				Node node = childNodes2.item(i);
				if(node.getNodeType()==node.ELEMENT_NODE){
					NamedNodeMap attributes = node.getAttributes();
					System.out.println(attributes.getNamedItem("id").getNodeValue());
					NodeList childNodes3 = node.getChildNodes();
					for(int j =0;j<childNodes3.getLength();j++){
						Node item = childNodes3.item(j);
						if(item.getNodeType()==item.ELEMENT_NODE){
							System.out.println(item.getNodeName()+"----"+item.getTextContent());
						}
					}
				}
			}
			
		} catch (ParserConfigurationException e) {		
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值