如何使用 Java 从 XML 读取值并将其保存在 Hashmap 中 [英] How can I read values from XML and save it in Hashmap using Java

查看:54
本文介绍了如何使用 Java 从 XML 读取值并将其保存在 Hashmap 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 XML 解析的新手,我尝试检索 XML 之后的数据并将其保存在哈希图中.我希望将每个字段的描述、ID 和名称保存在 Hashmap 中.

I'm new to XML parsing, I trying to retrieve data following XML n save it in hashmap. I want Description, id and name from each Fields to be saved in Hashmap.

 <Entities TotalResults="13689">
         <Entity Type="test">
              <Fields>
                   <Field Name="description">
                       <Value>I want to print THIS</Value>
                   </Field>
                   <Field Name="id"><Value>1357</Value></Field>
                   <Field Name="vc-comments"><Value></Value></Field>
                   <Field Name="name">
                       <Value>locked manager - lock state</Value>
                   </Field>
                   <Field Name="has-linkage"><Value>N</Value></Field>
               </Fields>
         </Entity>
         <Entity Type="test">
              <Fields>
                   <Field Name="description"><Value>Print this</Value></Field>
                   <Field Name="user-06"><Value></Value></Field>
                   <Field Name="id"><Value>1358</Value></Field>
                   <Field Name="name">
                       <Value>locked manager - stealing a key </Value>
                   </Field>
                   <Field Name="vc-status"><Value></Value></Field>
              </Fields>
         </Entity>
     </Entities>

推荐答案

当你不知道字段时应该使用hash map

You should use hash map when you dont know the fields

解决问题的仪式方式是构建一个 pojo 类像这样

The rite way for you problem is to build a pojo class like this

public class MyPojo
{
    private Entities Entities;

    public Entities getEntities ()
    {
        return Entities;
    }

    public void setEntities (Entities Entities)
    {
        this.Entities = Entities;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [Entities = "+Entities+"]";
    }
}


public class Entities
{
    private String TotalResults;

    private Entity[] Entity;//you can use List<> insted 

    public String getTotalResults ()
    {
        return TotalResults;
    }

    public void setTotalResults (String TotalResults)
    {
        this.TotalResults = TotalResults;
    }

    public Entity[] getEntity ()
    {
        return Entity;
    }

    public void setEntity (Entity[] Entity)
    {
        this.Entity = Entity;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [TotalResults = "+TotalResults+", Entity = "+Entity+"]";
    }
}

我制作了 2 个 pojos 以便您更好地理解

I have made 2 pojos for your better understanding

您可以创建与 xml 相关的其余部分.以后你就可以用

you can create the rest as related to xml. Later you can just use

            File file = new File("My.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(MyPojo.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            MyPojo myPojo = (MyPojo) jaxbUnmarshaller.unmarshal(file);
            System.out.println(myPojo);//get your value with getter setter.

//Description, id, name 可以检索.

//Description, id and name can be retrieved.

一般来说,你使用一个集合(List、Map、Set)来存储对象具有相似特征,这就是泛型存在的原因.

In general, you use a Collection (List, Map, Set) to store objects with similar characteristics, that's why generics exist.

这篇关于如何使用 Java 从 XML 读取值并将其保存在 Hashmap 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆