xml字符串解析为map(不需要导入其他jar包)

xml字符串解析为map


直接粘贴后改包名即可运行

// 包名自己改
package com.kun.leetcode.core.twe;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;

/**
 * @description:
 * @author: kunT
 * @create: 2022-02-09 10:13
 */
public class doc {
    public static void main(String[] args) {

        // 需要解析的xml字符串
        String xml = "<Request>\n" +
                "  <Header>\n" +
                "    <SourceSystem>SYS0009</SourceSystem>\n" +
                "    <MessageID>0001</MessageID>\n" +
                "  </Header>\n" +
                "  <Body>\n" +
                "    <RegisterDocumentRt>\n" +
                "      <OrganizationCode>48500030-7</OrganizationCode>\n" +
                "      <BusinessFieldCode>00002</BusinessFieldCode>\n" +
                "      <PATPatientID>0010841465</PATPatientID>\n" +
                "      <PAADMVisitNumber>109971386</PAADMVisitNumber>\n" +
                "    </RegisterDocumentRt>\n" +
                "  </Body>\n" +
                "</Request>";

        HashMap<String, String> map = xmlValueToMap(xml);
        System.out.println(map);
    }
    public static HashMap<String, String> xmlValueToMap(String xml) {

        // 使用jdk自带的工具将xml字符串转化为document
        HashMap<String, String> map = new HashMap<>();
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        Document parse = null;
        try {
            // 替换掉xml字符串中的空格和换行防止,清除错误的文本结点(可根据自己的需求修改不同替换元素)
            String replace = xml.replaceAll("\n", "").replace(" ", "");
            StringReader stringReader = new StringReader(replace);
            parse = documentBuilderFactory.newDocumentBuilder().parse(new InputSource(stringReader));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }

        Element documentElement = parse.getDocumentElement();

        valueToMap(map, documentElement);
        return map;
    }

    private static void valueToMap(HashMap<String, String> map, Element documentElement) {
        NodeList childNodes = documentElement.getChildNodes();

        for(int i = 0 ; i < childNodes.getLength(); i++) {
            Node item = childNodes.item(i);

            // 如果结点类型为3表示该结点为文本结点需要存入map,反之则继续遍历文档
            if(item.getNodeType() == 3) {
                // 将标签名为key, 文本节点的文本内容为value存入到map中
                map.put(documentElement.getNodeName(), item.getTextContent());
            } else {
                valueToMap(map, (Element) item);
            }
        }
    }

}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值