解析XML字符串存入Map输出

菜鸟自己写着玩

import org.springframework.web.bind.annotation.RequestBody;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
public class test06 {
    public static void generalXmlSync(@RequestBody String requestData) throws ParserConfigurationException, IOException, SAXException {
        //String xml 转Document xml
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(requestData));
        Document document = db.parse(is);
        //通过Document对象的getElementsByTagName()返根节点的一个list集合
        HashMap hashMap = new HashMap();
        HashMap hashMapData = new HashMap();
        NodeList booklist = document.getElementsByTagName("Aaa");
        Object Id = null;
        String values;
        for (int i = 0; i < booklist.getLength(); i++) {
            //循环遍历获取每一个book
            Node book = booklist.item(i);
            //通过Node对象的getAttributes()方法获取全的属性值
            NamedNodeMap bookmap = book.getAttributes();
            //循环遍每一个book的属性值
            for (int j = 0; j < bookmap.getLength(); j++) {
                Node node = bookmap.item(j);
                hashMapData.put(node.getNodeName(), node.getNodeValue());
                //通过Node对象的getNodeName()和getNodeValue()方法获取属性名和属性值
                NodeList booklis = document.getElementsByTagName("root");
                for (int a = 0; a < booklis.getLength(); a++) {
                    Element nodee;
                    nodee = (Element) booklis.item(a);

                    Node timeStamp = nodee.getElementsByTagName("TimeStamp").item(0);
                    values = timeStamp.getFirstChild() == null ? "" : timeStamp.getFirstChild().getNodeValue();
                    hashMap.put("TimeStamp", values);

                    Node accessCode = nodee.getElementsByTagName("AccessCode").item(0);
                    values = accessCode.getFirstChild() == null ? "" : accessCode.getFirstChild().getNodeValue();
                    hashMap.put("AccessCode", values);

                    Node userDiscernType = nodee.getElementsByTagName("UserDiscernType").item(0);
                    values = userDiscernType.getFirstChild() == null ? "" : userDiscernType.getFirstChild().getNodeValue();
                    hashMap.put("UserDiscernType", values);

                    Node userDiscernData = nodee.getElementsByTagName("UserDiscernData").item(0);
                    values = userDiscernData.getFirstChild() == null ? "" : userDiscernData.getFirstChild().getNodeValue();
                    hashMap.put("UserDiscernData", values);

                    Node roomId = nodee.getElementsByTagName("Id").item(0);
                    values = roomId.getFirstChild() == null ? "" : roomId.getFirstChild().getNodeValue();
                    hashMap.put("Id", values);
                }
                NodeList booklists = document.getElementsByTagName("bbb");
                //循环遍历获取每一个book
                Node books = booklists.item(i);
                //解析book节点的子节点
                NodeList childlist = books.getChildNodes();
                for (int t = 0; t < childlist.getLength(); t++) {
                    //区分出text类型的node以及element类型的node
                    if (childlist.item(t).getNodeType() == Node.ELEMENT_NODE) {
                        hashMapData.put(childlist.item(t).getNodeName(), childlist.item(t).getTextContent());
                        System.out.println(childlist.item(t).getNodeName()+":"+ childlist.item(t).getTextContent());
                    }
                }
                hashMap.put("data", hashMapData);
                System.out.println("解析xml存入map"+hashMap);
            }
        }
    }
    public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
String requestData = "\n" +
        "    2015-12-16 12:50:21\n" +
        "    AACC\n" +
        "    02\n" +
        "    null\n" +
        "    \n" +
        "        \n" +
        "            \n" +
        "                \n" +
        "                    11\n" +
        "                    22\n" +
        "                    2015-12-16 12:50:08\n" +
        "                    \n" +
        "                    1\n" +
        "                    \n" +
        "                \n" +
        "            \n" +
        "            \n" +
        "                \n" +
        "                    \n" +
        "                        4545\n" +
        "                        222\n" +
        "                        2015-12-16 12:50:08\n" +
        "                        111\n" +
        "                        \n" +
        "                        \n" +
        "                    \n" +
        "                \n" +
        "                \n" +
        "                    \n" +
        "                        \n" +
        "                            ssdasd\n" +
        "                            \n" +
        "                            \n" +
        "                            \n" +
        "                            \n" +
        "                        \n" +
        "                    \n" +
        "    \n" +
        "    1111111111111111\n" +
        "";
        generalXmlSync(requestData);
    }
}

你可能感兴趣的:(解析XML字符串存入Map输出)