Return map from xml file Using Jaxb Library Java

允我心安 提交于 2021-01-27 21:37:41

问题


I am trying to return a map directly from an XML file. I've tried it by creating a custom Adapter but the problem is this adapter is not receiving Input Parameter with values from XML.

For Referencing, here are the screenshots of the problem.

Here is the XML which I am trying to parse

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<GroupRoot>
    <Root>
        <Group>
            <UUID>ahsdlfkjadslkfjalkdsjflakjdslf</UUID>
            <Name>Root Group 1</Name>
            <Entry>
                <UUID>1212135454==</UUID>
                <String>
                    <Key>Notes</Key>
                    <Value>Notes from Manager</Value>
                </String>
                <String>
                    <Key>Item1</Key>
                    <Value>Item1Value</Value>
                </String>
                <String>
                    <Key>Item2</Key>
                    <Value>Item2Value</Value>
                </String>
                <String>
                    <Key>Item3</Key>
                    <Value>Item3Value</Value>
                </String>
                <String>
                    <Key>Item4</Key>
                    <Value>Item4Value</Value>
                </String>
            </Entry>
            <Entry>
                <UUID>45645466546546464==</UUID>
                <String>
                    <Key>Notes</Key>
                    <Value>Notes from Manager</Value>
                </String>
                <String>
                    <Key>Item1</Key>
                    <Value>Item1Value1</Value>
                </String>
                <String>
                    <Key>Item2</Key>
                    <Value>Item2Value1</Value>
                </String>
                <String>
                    <Key>Item3</Key>
                    <Value>Item3Value1</Value>
                </String>
                <String>
                    <Key>Item4</Key>
                    <Value>Item4Value1</Value>
                </String>
            </Entry>
        </Group>
    </Root>
</GroupRoot>

& Here is my Pojo's.

package com.parser.xml.model; 
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "GroupRoot")
public class KeePassFile {
    @XmlElement(name = "Root")
    public Root root;

}


package com.parser.xml.model; 
import java.util.List; 
import javax.xml.bind.annotation.XmlElement; 
public class Root {
    @XmlElement(name = "Group")
    public List<Group> groups;

}



package com.parser.xml.model;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
public class Group {

    @XmlElement(name = "Name")
    public String name;
    
    @XmlElement(name = "Group", nillable = false )
    public List<Group> subGroups;

    @XmlElement(name = "Entry",nillable = false)
    public List<Entry> entries;  
}


package com.parser.xml.model;

import java.util.HashMap;
import java.util.Map; 
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

public class Entry {

    @XmlElement(name = "UUID")
    public String UUID;
    
    @XmlElement(name = "String")
    @XmlJavaTypeAdapter(StringMapperAdapter.class)
    public Map<String, String> MappedItems;

}


class MapperString { 
    @XmlElement(name = "Key")
    public String key; 
    @XmlElement(name = "Value")
    public String value;

    public MapperString() {
    }

    public MapperString(String key, String value) {
        this.key = key;
        this.value = value;
    }
 
}

class StringMapperAdapter extends XmlAdapter<MapperString[], Map<String, String>> {

    @Override
    public Map<String, String> unmarshal(MapperString[] v) throws Exception {
        HashMap<String, String> map = new HashMap<String, String>(); 
        for (MapperString element : v) {
            map.put(element.key, element.value);
        } 
        return map;
    }

    @Override
    public MapperString[] marshal(Map<String, String> v) throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

}

Main Method

package com.parser.xml;

import java.io.File;
import java.io.IOException; 
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller; 
import org.springframework.core.io.ClassPathResource; 
import com.parser.xml.model.KeePassFile;

public class RunAppMain {

    private static final String FILE_PATH = "TestXML.xml";
    
    public static void main(String[] Args) throws IOException, JAXBException
    {
        File resource = new ClassPathResource(FILE_PATH).getFile();
        JAXBContext context = JAXBContext.newInstance(KeePassFile.class);
        Unmarshaller un = context.createUnmarshaller();
        KeePassFile entry = (KeePassFile) un.unmarshal(resource);

        System.out.println("Testing");
    }
}

回答1:


I don't know if it's possible to use an XmlAdapter on an xs:sequence. I did manage to set the XmlAdapter one level up on the Entry element:

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

public class Test {
  public static void main(String[] args) throws Exception {
    Root root = (Root) JAXBContext.newInstance(Root.class).createUnmarshaller().unmarshal(new File("test.xml"));
    System.out.println("" + root.entryList.get(0).map);
    System.out.println("" + root.entryList.get(1).map);
  }
}

@XmlRootElement(name = "Root")
class Root {
  @XmlElement(name = "Entry")
  public List<Entry> entryList = new ArrayList<>();
}

@XmlJavaTypeAdapter(EntryAdapter.class)
class Entry {
  public String              uuid;
  public Map<String, String> map = new TreeMap<>();
}

class EntryMappableByJaxb {
  @XmlElement
  public String         UUID;
  @XmlElement(name = "String")
  public List<KeyValue> stringList = new ArrayList<>();
}

class KeyValue {
  @XmlElement
  public String Key;
  @XmlElement
  public String Value;
}

class EntryAdapter extends XmlAdapter<EntryMappableByJaxb, Entry> {
  @Override
  public Entry unmarshal(EntryMappableByJaxb entryMappableByJaxb) throws Exception {
    Entry entry = new Entry();
    entry.uuid = entryMappableByJaxb.UUID;
    entryMappableByJaxb.stringList.forEach(i -> entry.map.put(i.Key, i.Value));
    return entry;
  }

  @Override
  public EntryMappableByJaxb marshal(Entry v) throws Exception {
    return null;
  }
}

Test XML used:

<Root>
  <Entry>
    <UUID>uuid 1</UUID>
    <String>
      <Key>1.1</Key>
      <Value>value 1.1</Value>
    </String>
    <String>
      <Key>1.2</Key>
      <Value>value 1.2</Value>
    </String>
  </Entry>
  <Entry>
    <UUID>uuid 2</UUID>
    <String>
      <Key>2.1</Key>
      <Value>value 2.1</Value>
    </String>
    <String>
      <Key>2.2</Key>
      <Value>value 2.2</Value>
    </String>
  </Entry>
</Root>


来源:https://stackoverflow.com/questions/65288722/return-map-from-xml-file-using-jaxb-library-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!