You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.7 KiB
Plaintext

<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="java.util.*" %>
<%@ page import="com.engine.common.util.ParamUtil" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="java.sql.Array" %>
<%@ page import="com.wbi.util.Util" %>
<%@ page import="java.math.BigDecimal" %>
<%@ page import="tebie.applib.api.O" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="javax.xml.parsers.SAXParserFactory" %>
<%@ page import="javax.xml.parsers.SAXParser" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.io.ByteArrayInputStream" %>
<%@ page import="com.engine.util.XMLUtils" %>
<%@ page import="org.xml.sax.helpers.DefaultHandler" %>
<%@ page import="org.xml.sax.Attributes" %>
<%@ page import="org.xml.sax.SAXException" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%
%>
<%!
public static Map<String, String> parseXMLToMap(String xmlString) {
new BaseBean().writeLog("XMLUtils:xmlString==>"+xmlString);
Map<String, String> resultMap = new HashMap<>();
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
InputStream xmlInput = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
saxParser.parse(xmlInput, new XMLUtils.SAXHandler(resultMap));
} catch (Exception e) {
e.printStackTrace();
}
return resultMap;
}
private static class SAXHandler extends DefaultHandler {
private Map<String, String> resultMap;
private Stack<String> elementStack = new Stack<>();
private StringBuilder elementValue = new StringBuilder();
public SAXHandler(Map<String, String> resultMap) {
this.resultMap = resultMap;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
elementStack.push(qName);
elementValue.setLength(0); // 清空字符缓存
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
String value = elementValue.toString().trim();
if (!value.isEmpty()) {
// 如果当前元素有值且没有子元素则将其添加到结果Map中
if (elementStack.size() > 1) {
resultMap.put(elementStack.peek(), value);
}
}
elementStack.pop();
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
elementValue.append(ch, start, length);
}
}
%>