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文件 ,和文件流。
可以得到节点值,节点属性值,(如果存在多个<img 下面代码还存在问题)
- 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"));
- }
- }
解决多个节点问题,,比如下面的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/5/6/95/0.jpg"/>
- <audio src="/contentlib/5/6/95/0.wav"/>
- <text region="Text" src="/contentlib/5/6/95/0.txt"/>
- </par>
- <par dur = "5000ms">
- <img region="Image" src="/contentlib/5/6/95/1.gif"/>
- <audio src="/contentlib/5/6/95/1.wav"/>
- <text region="Text" src="/contentlib/5/6/95/1.txt"/>
- </par>
- <par dur = "5000ms">
- <img region="Image" src="/contentlib/5/6/95/2.gif"/>
- <audio src="/contentlib/5/6/95/2.wav"/>
- <text region="Text" src="/contentlib/5/6/95/2.txt"/>
- </par>
- </body>
- </smil>
要得出所有的src值,下面代码可以实现
- import java.io.InputStream;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.List;
- 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;
- /**
- * Description: xml解析类
- * @author wayfoon
- * @version
- */
- 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;
- }
- /**
- * 得到某一节点指定属性的值
- * @param node
- * @param name
- * @param attrName
- * @return
- */
- public String getAttrByNode(Node node, String name, String attrName)
- {
- NodeList list = node.getChildNodes();
- // System.out.println("---"+list.getLength());
- String result = "";
- Node child = null;
- for (int innerLoop = 0; innerLoop < list.getLength(); innerLoop++)
- {
- child = list.item(innerLoop);
- if (child instanceof Element)
- {
- if ((child != null) && (child.getNodeName() != null)
- && (child.getNodeName().equals(name)))
- {
- // 遍历整个xml某节点指定的属性
- NamedNodeMap attrs = child.getAttributes();
- if (attrs.getLength() > 0 && attrs != null)
- {
- Node attr = attrs.getNamedItem(attrName);
- result = attr.getNodeValue();
- }
- }
- }
- }
- return result;
- }
- /**
- * 得到节点属性值,节点可能存在多个,返回的是个数组
- *
- * @param name
- * @param attrName
- * @return
- */
- public String[] getAttr(String name, String attrName)
- {
- int len = 0;
- String result[] = new String[] {};
- ArrayList resultlist = findNodes(name);
- if ((resultlist != null) && (resultlist.size() > 0))
- {
- result = new String[resultlist.size() * 3];
- 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)))
- {
- String img = getAttrByNode(node, "img", "src");
- String audio = getAttrByNode(node, "audio", "src");
- String text = getAttrByNode(node, "text", "src");
- if (!"".equals(img) && img != null)
- {
- result[len++] = img;
- }
- else
- {
- result[len++] = "";
- }
- if (!"".equals(audio) && audio != null)
- {
- result[len++] = audio;
- }
- else
- {
- result[len++] = "";
- }
- if (!"".equals(text) && text != null)
- {
- result[len++] = text;
- }
- else
- {
- result[len++] = "";
- }
- // System.out.println(result[len]);
- }
- }
- }
- }
- //System.out.println(result.length);
- return result;
- }
- public static void main(String[] args)
- {
- String file = "D://**//smil.smil";
- XmlParser xml = new XmlParser(file);
- // XmlParser xml1 =new XmlParser(file1);
- String[] strings = xml.getAttr("par", "dur");
- for (int i = 0; i < strings.length; i++)
- {
- System.out.println(strings[i]);
- }
- }
- }
输入结果:就是我们想得到的