JAXP usage

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;

public class Test {

	public static void main(String[] args) {
		String source = "<Customer_Products><result>0</result><resultMsg>查询成功</resultMsg></Customer_Products>";
		InputStream is = null;
		try {
			is = new ByteArrayInputStream(source.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		Document document = null;
		DocumentBuilder builder = null;
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			builder = factory.newDocumentBuilder();
			document = builder.parse(is);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("第一个resultMsg消息是:"
				+ document.getFirstChild().getChildNodes().item(1)
						.getTextContent());
	}

}

 

jaxp means java api for xml processing, it's obviously more convenient than dom4j, jdom etc..

 

The DOM parser is called a DocumentBuilder , as it builds an in-memory Document representation. The javax.xml.parsers.DocumentBuilder is created by the javax.xml.parsers.DocumentBuilderFactory . The DocumentBuilder creates an org.w3c.dom.Document instance, which is a tree structure containing nodes in the XML Document. Each tree node in the structure implements the org.w3c.dom.Node interface. There are many different types of tree nodes, representing the type of data found in an XML document. The most important node types are:

  • element nodes that may have attributes
  • text nodes representing the text found between the start and end tags of a document element.

from: http://en.wikipedia.org/wiki/Java_API_for_XML_Processing

 

你可能感兴趣的:(java,xml)