用 org.w3c.dom 包下 java自带的类 解析小XML字符串

java解析XML有很多方式,dom4j jdom等等

今天发现org.w3c.dom 是java自带的类包。而且比较简单


import java.io.ByteArrayInputStream;

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;


DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
String value = "";
try {
  DocumentBuilder db = factory.newDocumentBuilder();
  ByteArrayInputStream is = new ByteArrayInputStream(ele.getXML().getBytes());
  Document document = db.parse(is);//InputStream or File Object
  NodeList nodeList = document.getElementsByTagName("SYSTYPE");
  String value = "";
  for(int i=0; i

你可能感兴趣的:(java)