package com.example.lxb.awaken.model; import com.example.lxb.awaken.SpeechApp; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; /** * 使用Sax解析xml文档 ** Created by lxb on 2017/4/26. */ public class ParseXMLSAX { private static ParseXMLSAX parseXMLSAX; public static ParseXMLSAX getInstance(){ if(parseXMLSAX == null){ parseXMLSAX = new ParseXMLSAX(); } return parseXMLSAX; } /** * 获取assets文件 * * @param path * @return */ public InputStream getFileInputStream(String path) { try { return SpeechApp.getInstance().getAssets().open(path); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 解析xml文档 * * @param path * @return * @throws Exception */ public List
parse(String path) throws Exception { InputStream is = getFileInputStream(path); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); SaxXmlHandler handler = new SaxXmlHandler(); parser.parse(is, handler); return handler.getvoiceEntitys(); } /** * DefaultHandler是一个事件处理器,可以接收解析器报告的所有事件,处理所发现的数据 * */ private class SaxXmlHandler extends DefaultHandler { private List voiceEntitys; private VoiceEntity voiceEntity; private StringBuilder builder; public List getvoiceEntitys() { return voiceEntitys; } @Override public void startDocument() throws SAXException { // TODO Auto-generated method stub super.startDocument(); voiceEntitys = new ArrayList<>(); builder = new StringBuilder(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { // TODO Auto-generated method stub super.startElement(uri, localName, qName, attributes); if (localName.equals("item")) { voiceEntity = new VoiceEntity(); } builder.setLength(0); } @Override public void characters(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub super.characters(ch, start, length); builder.append(ch, start, length); //将读取的字符数组追加到builder中 } @Override public void endElement(String uri, String localName, String qName) throws SAXException { // TODO Auto-generated method stub super.endElement(uri, localName, qName); if (localName.equals("id")) { voiceEntity.setID(Integer.parseInt(builder.toString())); } else if (localName.equals("type")) { voiceEntity.setType(Integer.parseInt(builder.toString())); } else if (localName.equals("key")) { voiceEntity.setKey(Integer.parseInt(builder.toString())); } else if (localName.equals("cmd")) { voiceEntity.setCmd(builder.toString()); } else if (localName.equals("item")) { voiceEntitys.add(voiceEntity); } } } }
对外接口类:
package com.example.lxb.awaken.speech.util; import com.example.lxb.awaken.SpeechApp; import com.example.lxb.awaken.model.ParseXMLSAX; import com.example.lxb.awaken.model.VoiceEntity; import com.iflytek.cloud.util.ResourceUtil; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; /** * Xml结果解析类 */ public class XmlParser { public static String parseNluResult(String xml) { StringBuffer buffer = new StringBuffer(); try { // DOM builder DocumentBuilder domBuilder = null; // DOM doc Document domDoc = null; // init DOM DocumentBuilderFactory domFact = DocumentBuilderFactory.newInstance(); domBuilder = domFact.newDocumentBuilder(); InputStream is = new ByteArrayInputStream(xml.getBytes()); domDoc = domBuilder.parse(is); // 获取根节点 Element root = (Element) domDoc.getDocumentElement(); Element raw = (Element)root.getElementsByTagName("rawtext").item(0); buffer.append("【识别结果】" + raw.getFirstChild().getNodeValue()); buffer.append("\n"); Element e = (Element)root.getElementsByTagName("result").item(0); Element focus = (Element)e.getElementsByTagName("focus").item(0); buffer.append("【FOCUS】" + focus.getFirstChild().getNodeValue()); buffer.append("\n"); Element action = (Element)e.getElementsByTagName("action").item(0); Element operation = (Element)action.getElementsByTagName("operation").item(0); buffer.append("【ACTION】" + operation.getFirstChild().getNodeValue()); buffer.append("\n"); }catch(Exception e){ e.printStackTrace(); }; buffer.append("【ALL】" + xml); return buffer.toString(); } /** * 使用DOM解析方式 * DOM是基于树形结构的的节点或信息片段的集合,允许开发人员使用DOM API遍历XML树、检索所需数据。 * 分析该结构通常需要加载整个文档和构造树形结构,然后才可以检索和更新节点信息。 * 由于DOM在内存中以树形结构存放,因此检索和更新效率会更高。但是对于特别大的文档,解析和加载整个文档将会很耗资源。 * * * @param path * @return */ public static ListparseXMLByDom(String path){ InputStream is = null; try { is = SpeechApp.getInstance().getAssets().open(path); List s = new ArrayList<>(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //取得DocumentBuilderFactory实例 DocumentBuilder builder = factory.newDocumentBuilder(); //从factory获取DocumentBuilder实例 Document doc = builder.parse(is); //解析输入流 得到Document实例 Element rootElement = doc.getDocumentElement(); NodeList items = rootElement.getElementsByTagName("item"); for (int i = 0; i < items.getLength(); i++) { VoiceEntity depart = new VoiceEntity(); Node item = items.item(i); NodeList properties = item.getChildNodes(); for (int j = 0; j < properties.getLength(); j++) { Node property = properties.item(j); String nodeName = property.getNodeName(); if (nodeName.equals("ID")) { depart.setID(Integer.parseInt(property.getFirstChild().getNodeValue())); } else if (nodeName.equals("type")) { depart.setType(Integer.parseInt(property.getFirstChild().getNodeValue())); } else if (nodeName.equals("key")) { depart.setKey(Integer.parseInt(property.getFirstChild().getNodeValue())); } } s.add(depart); } return s; } catch (IOException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } return null; } /** * SAX:优点是解析速度快,占用内存少。非常适合在Android移动设备中使用 * * @param path */ public static List parseXMLBySAX(String path){ try { return ParseXMLSAX.getInstance().parse(path); } catch (Exception e) { e.printStackTrace(); } return null; } }
信息实体类:
package com.example.lxb.awaken.model; /** * Created by lxb on 2017/4/26. */ public class VoiceEntity extends XMLBase{ private int ID; private int type; private int key; private String cmd; public int getID() { return ID; } public void setID(int ID) { this.ID = ID; } public int getType() { return type; } public void setType(int type) { this.type = type; } public int getKey() { return key; } public void setKey(int key) { this.key = key; } public String getCmd() { return cmd; } public void setCmd(String cmd) { this.cmd = cmd; } }
测试接口:
long start = System.currentTimeMillis(); try { System.out.println("88-----------"+XmlParser.parseXMLBySAX("volinfo.xml").size()); //文件记得放在assets目录下 long end = System.currentTimeMillis(); } catch (Exception e) { e.printStackTrace(); }
volinfo.xml文件内容:
xml version="1.0" encoding="UTF-8"?>0 1 518 no 1 1 517 no 2 1 520 no 3 1 519 no 4 0 506 reboot -p 5 0 506 no 6 1 601 no 7 1 601 no 8 1 600 no 9 0 506 am start -n com.actions.demomovie/.FileBrowserActivity 10 0 506 am start -n com.android.settings/.Settings 11 1 602 am start -n com.android.browser/.BrowserActivity 12 0 506 am start -n com.android.calculator2/.Calculator 13 0 506 am start -n com.android.gallery3d/.app.GalleryActivity 14 1 506 no 15 1 506 no 16 0 506 am start -n com.actions.arlauncher/.AllAppActivity 17 0 506 am start -n com.android.email/.activity.Welcome 18 1 506 no 19 0 506 am start -n com.tencent.mm/.ui.LauncherUI 20 0 506 am start -n com.tencent.mobileqq/.activity.SplashActivity 21 0 506 no 22 0 506 no 23 0 506 no 24 0 506 no 25 0 506 no 26 1 506 no 27 0 506 no 28 1 506 no 29 1 506 no 30 0 506 no 31 0 506 no 32 1 506 no 33 0 506 am start -n com.android.settings/.Settings\$WifiSettingsActivity 34 1 506 no 35 1 506 no 36 1 506 no 37 1 506 no 38 1 506 no 39 1 502 no