java分别采用dom,sax,jdom,dom4j操作xml

注明:其中某些注释部分为读写文件(与之对应的是仅仅操作字符串),或者是可替换方式,在JDOM和DOM4J中XPATH需要用到jaxen.jar

一、DOM操作方式

	public static void getDoc() throws ParserConfigurationException, SAXException, IOException
	{
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		
		//Document doc = db.parse(new File("student.xml"));
		
		String xml = "";//xml string
		Document doc = db.parse(new InputSource(new StringReader(xml)));
		Element element  = doc.getDocumentElement();
		
		parseDoc(element);
	}
	
	public static void parseDoc(Node element)
	{
		System.out.print("<");
		System.out.print(element.getNodeName());
		
		NamedNodeMap al = element.getAttributes();
		
		for(int i = 0 ; i < al.getLength();i++)
		{
			Node node = al.item(i);
			System.out.print("  ");
			System.out.print(node.getNodeName());
			System.out.print("='");
			System.out.print(node.getNodeValue());
			System.out.print("'");
		}
		
		System.out.print(">");
		
		NodeList nl = element.getChildNodes();
		
		for(int i = 0 ;  i < nl.getLength();i++)
		{
			Node node = nl.item(i);
			if(node.getNodeType() == Node.ELEMENT_NODE)
			{
				parseDoc(node);
			}
			else if(node.getNodeType() == Node.TEXT_NODE)
			{
				System.out.print(node.getNodeValue());
			}
		}
		
		System.out.print("</");
		System.out.print(element.getNodeName());
		System.out.print(">");
	}

二、SAX操作方式

	/**
	 * @param args
	 * @throws SAXException 
	 * @throws ParserConfigurationException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException
	{
		SAXParserFactory saxFactory = SAXParserFactory.newInstance();
		SAXParser saxParser = saxFactory.newSAXParser();
//		saxParser.parse(new File("student.xml"), new Handler());
		String xml = "";//xml String
		InputSource   is=new   InputSource(new   StringReader(xml)); 
		saxParser.parse(is,  new Handler());
	}
	

}

class Handler extends DefaultHandler
{
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException
	{
		System.out.print("<");
		System.out.print(qName);
		
		for(int i = 0 ; i < attributes.getLength();i++)
		{
			String name = attributes.getQName(i);
			String value = attributes.getValue(i);
			
			System.out.print(" "+name + "='" + value+"'");
		}
		
		System.out.print(">");
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException
	{
		System.out.print(new String(ch,start,length));
	}
	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException
	{
		System.out.print("</");
		System.out.print(qName);
		System.out.print(">");
	}

三、JDOM操作方式

/**
	 * @param args
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 * @throws JDOMException 
	 */
	public static void main(String[] args) throws FileNotFoundException, IOException, JDOMException
	{
		Document doc = new Document();
		
		doc = doc.addContent(new Element("学生名册"));
		
		Element element = doc.getRootElement();

		element.addContent(new Element("学生").setAttribute(
				new Attribute("学号", "1")).addContent(
				new Element("姓名").setText("张三")).addContent(
				new Element("性别").setText("男")).addContent(
				new Element("年龄").setText("20")));


		XMLOutputter out = new XMLOutputter();
		//out.output(doc, new FileOutputStream("student.xml"));
		StringWriter sw = new StringWriter();
		out.output(doc, sw);
		String xml =sw.toString();
		SAXBuilder saxBuilder = new SAXBuilder();
		//Document doc1= saxBuilder.build(new File("student.xml"));
		Document doc1 = saxBuilder.build(new StringReader(xml));
		Element root = doc1.getRootElement();
		print(root);
		
		
		/**
		 * XPath,需要添加jaxen.jar包
		 */
		
		List<Element> elements = XPath.selectNodes(root,"//姓名");
		System.out.println(elements);
	}
	
	
	public static void print(Element element)
	{
		System.out.println(element);
		List<Element> elements = element.getChildren();
		for(Element e : elements)
		{
			print(e);
		}
		
	}

四、Dom4j操作方式

/**
	 * @param args
	 * @throws IOException 
	 * @throws DocumentException 
	 */
	public static void main(String[] args) throws IOException, DocumentException
	{
		Element element = DocumentHelper.createElement("学生名册");
		Document doc = DocumentHelper.createDocument(element);
	
		Element stu_element = element.addElement("学生");
		stu_element.addAttribute("学号", "1");
		Element name_element = stu_element.addElement("姓名");
		name_element.setText("张三");
		Element sex_element = stu_element.addElement("性别");
		sex_element.setText("男");
		Element age_element = stu_element.addElement("年龄");
		age_element.setText("20");
		
		OutputFormat of = new OutputFormat("    ", true);
		StringWriter sw = new StringWriter();
		XMLWriter xmlwriter = new XMLWriter(sw,of);
		xmlwriter.write(doc);
		String xml  = sw.toString();
		System.out.println(xml);
		
//		XMLWriter xmlwriter = new XMLWriter(new FileOutputStream("dom4j_student.xml"),of);
//		xmlwriter.write(doc);
		
		
		SAXReader saxReader = new SAXReader();
		Document doc1 = saxReader.read(new StringReader(xml));
		//Document doc1 = saxReader.read(new FileInputStream("dom4j_student.xml"));
		print(doc1.getRootElement());
		
		
		/**
		 * XPath,需要添加jaxen.jar包
		 */
		List<? extends Node> elements = doc.selectNodes("//姓名");
//		XPath xpath = new DefaultXPath("//姓名");
//		List<? extends Node> elements = xpath.selectNodes(element);
		System.out.println(elements);
	}
	
	public static void print(Element element)
	{
		System.out.println(element);
		List<Element> elements = element.elements();
		for(Element e : elements)
		{
			print(e);
		}
		
	}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值