小编yan*_*she的帖子

如何解析xml到hashmap?

我有一个我要解析的xml示例

 <?xml version="1.0" encoding="utf-8"?>

<Details>
    <detail-a>

        <detail> attribute 1 of detail a </detail>
        <detail> attribute 2 of detail a </detail>
        <detail> attribute 3 of detail a </detail>

    </detail-a>

    <detail-b>
        <detail> attribute 1 of detail b </detail>
        <detail> attribute 2 of detail b </detail>

    </detail-b>


</Details>
Run Code Online (Sandbox Code Playgroud)

我想从这个xml编写一个方法,将它解析为hashmap,键是一个字符串,值是一个字符串列表.

例如:键"detail a"value = {"详细信息的属性1","详细信息的属性2","详细信息的属性3"}

等等..

做这个的最好方式是什么 ?因为我很困惑:

我到目前为止试图打印细节-a和细节-b但是我感到空白......

public static void main(String[] args) {
    // TODO Auto-generated method stub

    DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();

        DocumentBuilder builder;
        try {
            builder = factory.newDocumentBuilder();
            File f= new File("src/Details.xml");
            Document doc=builder.parse(f); …
Run Code Online (Sandbox Code Playgroud)

java xml

6
推荐指数
2
解决办法
2万
查看次数

标签 统计

java ×1

xml ×1