xml文件如下:
<smil xmlns="http://www.w3.org/2000/SMIL20/CR/Language">
<head>
<layout>
<root-layout height="100%" width="100%" />
<region id="Image" top="0" left="0" height="80%" width="100%"/>
<region id="Text" top="80%" left="0" height="20%" width="100%"/>
</layout>
</head>
<body>
<par dur = "5000ms">
<img region="Image" src="/contentlib/32/35/92/52.jpg"/>
<audio src="/contentlib/32/36/58.wav"/>
<text region="Text" src="/contentlib/1/3/29/12.txt"/>
</par>
</body>
</smil>
要解析出xml文件节点属性,比如 要得出 节点<img region="Image" src="/contentlib/32/35/92/52.jpg"/> 属性src的值(/contentlib/32/35/92/52.jpg)。
下面是源码,可以直接解析xml文件 ,和文件流。
package com.huawei.idea.mkt.common;
import java.io.InputStream;
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.FactoryConfigurationError;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
/**
* <p>Description: xml解析类</p>
*
*/
public class XmlParser
{
private static DocumentBuilderFactory factory;
private static DocumentBuilder builder;
private Document doc;
private Element root;
private String fileName;
private InputStream inputstream;
/**
* 解析文件xml
* @param file String
*/
public XmlParser(String file)
{
if (file == null)
{
return;
}
this.fileName = file;
try
{
load();
}
catch (IOException ex)
{
ex.printStackTrace();
return;
}
}
/**
* 解析输入流xml
* @param input InputStream
*/
public XmlParser(InputStream input)
{
if (input == null)
{
return;
}
inputstream = input;
try
{
loadStream();
}
catch (IOException ex)
{
ex.printStackTrace();
return;
}
}
/**
* 获取XML文件中指定标签所对应的节点集合
* @param key String 标签名字
* @return ArrayList 指定标签对应的节点集
*/
public ArrayList findNodes(String key)
{
NodeList nodes = root.getElementsByTagName(key);
ArrayList nodeList = new ArrayList();
for (int i = 0; i < nodes.getLength(); i++)
{
nodeList.add(i, (Node) nodes.item(i));
}
return nodeList;
}
/**
* 初始化XML
* @throws IOException
*/
public void loadStream()
throws IOException
{
try
{
loadXMLParser();
doc = builder.parse(inputstream);
root = doc.getDocumentElement();
}
catch (IOException ex)
{
ex.printStackTrace();
}
catch (SAXException ex)
{
ex.printStackTrace();
}
finally
{
inputstream.close();
}
}
/**
* 初始化XML
* @throws IOException
*/
public void load()
throws IOException
{
try
{
loadXMLParser();
doc = builder.parse(fileName);
root = doc.getDocumentElement();
}
catch (SAXException SaxEx)
{
SaxEx.printStackTrace();
throw new IOException(SaxEx.getMessage() + "XML file parse error:"
+ SaxEx.getException());
}
catch (IOException IoEx)
{
IoEx.printStackTrace();
throw new IOException(IoEx.getMessage() + "XML file parse error:");
}
catch (Exception ex)
{
ex.printStackTrace();
throw new IOException(ex.getMessage() + "XML file parse error:");
}
}
/**
* 初始化XML
* @throws IOException
*/
private void loadXMLParser()
throws IOException
{
if (builder == null)
{
try
{
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
}
catch (ParserConfigurationException ex)
{
throw new IOException("XML Parser load error:"
+ ex.getLocalizedMessage());
}
catch (FactoryConfigurationError ConfErrEx)
{
throw new IOException("XML Parser load error:"
+ ConfErrEx.getLocalizedMessage());
}
catch (Exception Ex)
{
throw new IOException("XML Parser load error:"
+ Ex.getLocalizedMessage());
}
}
}
/**
* 获取XML文件中某级节点下一级元素的值
* @param node 节点对象
* @param subTagName subTagName元素的标签名
* @return String 该标记元素的的内容
*/
public static String getSubTagValue(Node node, String subTagName)
{
String returnString = "";
if ((node != null) && (subTagName != null))
{
NodeList children = node.getChildNodes();
for (int innerLoop = 0; innerLoop < children.getLength(); innerLoop++)
{
Node child = children.item(innerLoop);
if ((child != null) && (child.getNodeName() != null)
&& (child.getNodeName().equals(subTagName)))
{
Node grandChild = child.getFirstChild();
if (grandChild != null)
{
return grandChild.getNodeValue();
}
}
}
}
return returnString;
}
/**
* 获取XML文件中某单一值
* @param node 节点对象
* @param subTagName subTagName元素的标签名
* @return String 该标记元素的的内容
*/
public String getresult(String name)
{
String result = "";
ArrayList resultlist = findNodes(name);
if ((resultlist != null) && (resultlist.size() > 0))
{
for (int i = 0; i < resultlist.size(); i++)
{
Node node = (Node) resultlist.get(i);
if (node instanceof Element)
{
if ((node != null) && (node.getNodeName() != null)
&& (node.getNodeName().equals(name)))
{
Node grandChild = node.getFirstChild();
if (grandChild != null)
{
result = grandChild.getNodeValue();
}
}
}
}
}
return result;
}
//
public String getAttr(String name,String attrName)
{
String result = "";
ArrayList resultlist = findNodes(name);
if ((resultlist != null) && (resultlist.size() > 0))
{
for (int i = 0; i < resultlist.size(); i++)
{
Node node = (Node) resultlist.get(i);
if (node instanceof Element)
{
if ((node != null) && (node.getNodeName() != null)
&& (node.getNodeName().equals(name)))
{
//遍历整个xml某节点指定的属性
NamedNodeMap attrs=node.getAttributes();
if(attrs.getLength()>0 && attrs!=null)
{
Node attr=attrs.getNamedItem(attrName);
result=attr.getNodeValue();
}
}
}
}
}
return result;
}
public static void main(String[] args)
{
String file="D:\\common\\Tomcat60\\smil.smil";
XmlParser xml =new XmlParser(file);
System.out.println(xml.getAttr("text","src"));
}
}