利用java Api解析XML
来源:互联网 发布:美国最新api数据 编辑:程序博客网 时间:2024/06/02 18:17
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
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.InputSource;
import org.xml.sax.SAXException;
/**
* Usage: You can get a instance of Document from a string of xml.
* This class supply some method to read a document.
*
*/
public class XMLParser {
/**
* Get a instance of Document from a string of xml.
*
* @param xmlStr
* @return Document
*/
public static Document parse(String xmlStr) {
if (xmlStr == null) {
return null;
}
StringReader reader = new StringReader(xmlStr);
InputSource source = new InputSource(reader);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
if(builder == null){
return null;
}
Document doc = null;
try {
doc = builder.parse(source);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return doc;
}
/**
* Get Item Value.
* @param node
* @return String
*/
public static String getItemValue(Node node){
String value = "";
if(node == null){
return null;
}
node = node.getFirstChild();
if(node != null){
value = node.getNodeValue();
}
return value.trim();
}
/**
* Get Simple Node's Value
*
* @param list
* @return String
*/
public static String getSimpleNodeValue(NodeList list){
String value = "";
Node node = null;
if (list != null) {
node = list.item(0);
value = getItemValue(node);
}
return value.trim();
}
/**
* Get Children.
* @param list
* @return List<Node>
*/
public static List<Node> getChildren(NodeList list){
List<Node> children = new ArrayList<Node>();
Node node = null;
if(list == null){
return children;
}
for(int i= 0; i< list.getLength(); i++){
node = list.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
children.add(node);
}
}
return children;
}
/**
* Get Attribute Value
* @param node
* @param name
* @return String
* @throws Exception
*/
public static String getAttributeValue(Node node, String name) throws Exception{
String value = "";
if(node == null){
throw new Exception("Node is null!");
}
NamedNodeMap nnm = node.getAttributes();
if (nnm == null) {
throw new Exception("NamedNodeMap is null!");
}
Node item = nnm.getNamedItem(name);
if (item == null) {
throw new Exception("No such attribute.");
}
value = item.getNodeValue();
return value.trim();
}
}
- 利用java Api解析XML
- Java 解析XML API
- Java API 解析读写XML
- Java API 解析XML文件
- Php利用java解析xml
- Php利用java解析xml
- Java 利用Xstream 解析 XML
- Dom4j 解析 XML(Java Api)
- Php利用java解析xml[原创]
- 利用java Dom解析XML文件
- 利用java的Dom解析XML文件
- JAVA语言利用DOM解析XML文件
- Java中利用simpleFramework解析xml
- Java中利用simpleFramework解析xml
- JAVA中利用DOM解析XML文档
- java 利用dom4j 解析XML文件
- Java利用Sax解析xml文件
- java利用dom4j解析XML文件
- SAP 高级应用开发 学习笔记 1 -- RFC NO.7
- C# 中的委托和事件(转)
- 历史数据转移
- java.lang.OutOfMemoryError: Java heap space
- 折腾 VS2008 + ReportViewer + CrystalReports + PrintControl.cab 实现水晶报表本地打印
- 利用java Api解析XML
- 推荐的书:windows程序调试
- SQL Server2000全文检索应用实例
- 2.3 TcpListener同步基础服务器开发
- 什么是公平
- Wireless Router with a 3G/UMTS/HSDPA dongle (OPENWRT 无线网络连接)
- ServletContext与ServletConfig的深度分析
- 解决该页正在访问其控制范围之外的数据。这有些危险。是否继续?
- Lazy Function Definition Pattern