Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将hashmap的键和值设置为xml文件的标记和值_Java_Xml_Jaxb_Eclipselink - Fatal编程技术网

Java 如何将hashmap的键和值设置为xml文件的标记和值

Java 如何将hashmap的键和值设置为xml文件的标记和值,java,xml,jaxb,eclipselink,Java,Xml,Jaxb,Eclipselink,我遵循并编写了如下代码 请求和响应XML <AlphabetReq> <a>Apple</a> <b>Ball</b> <c>Cat</c> <d>Dog</d> <e>Elephant</e> <f>Fox</f> </AlphabetReq> <AlphabetResp

我遵循并编写了如下代码

请求和响应XML

<AlphabetReq>
    <a>Apple</a>
    <b>Ball</b>
    <c>Cat</c> 
    <d>Dog</d>
    <e>Elephant</e>
    <f>Fox</f>
</AlphabetReq>

<AlphabetResp>
    <a>Apple</a>
    <b>Ball</b>
    <c>Cat</c> 
    <d>Dog</d>
    <e>Elephant</e>
    <f>Fox</f>
</AlphabetResp>

苹果
球
猫
狗
大象
福克斯
苹果
球
猫
狗
大象
福克斯
AlphabetReq和AlphabetResp类

import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Set;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Alph {

@XmlPath(".")
@XmlJavaTypeAdapter(AlphAdapter.class)
private LinkedHashMap<String, String> cLinkedHashMap = new 
        LinkedHashMap<String, String>();

@XmlPath(".")
@XmlJavaTypeAdapter(AlphAdapter.class)
private LinkedHashMap<String, String> gLinkedHashMap = new 
        LinkedHashMap<String, String>();

protected void put(String theKey, String theValue) {
    cLinkedHashMap.put(theKey, theValue);   
    gLinkedHashMap.put(theKey, theValue);
}

protected String get(String theKey) {
    return (String) cLinkedHashMap.get(theKey);
}

protected Set<Entry<String,String>> getCEntrySet() {
    return cLinkedHashMap.entrySet();
}

protected Set<Entry<String,String>> getGEntrySet() {
    return gLinkedHashMap.entrySet();
}

protected LinkedHashMap<String, String> getCLinkedHashMap() {
    return cLinkedHashMap;
}

protected LinkedHashMap<String, String> getGLinkedHashMap() {
    return gLinkedHashMap;
}

public String toCXML() throws XMLHandlingException {
    return null;
}

public String toGXML() throws XMLHandlingException {
    return null;
}

}

@XmlRootElement(name="AlphReq")
@XmlDiscriminatorValue("AlphabetReq")
@XmlAccessorType(XmlAccessType.FIELD)
public class AlphabetReq extends Alph {

public static AlphabetReq getInstance(String theAlphabetReqXML) throws 
XMLHandlingException {
    return XMLUtils.parseAlphabetReqXML(theAlphabetReqXML);
}

public String toCXML() throws XMLHandlingException {
    return XMLUtils.getAlphabetReqXML(this);
}

}

@XmlRootElement(name="AlphResp")
@XmlDiscriminatorValue("AlphabetResp")
@XmlAccessorType(XmlAccessType.FIELD)
public class AlphabetResp extends Alph {

public static AlphabetResp getInstance(String theAlphabetRespXML) throws 
XMLHandlingException {
    return XMLUtils.parseAlphabetRespXML(theAlphabetRespXML);
}

public String toCXML() throws XMLHandlingException {
    return XMLUtils.getAlphabetRespXML(this);
}

}
import java.util.LinkedHashMap;
导入java.util.Map.Entry;
导入java.util.Set;
导入javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
导入org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
公共级Alph{
@XmlPath(“.”)
@XmlJavaTypeAdapter(AlphAdapter.class)
私有LinkedHashMap cLinkedHashMap=新建
LinkedHashMap();
@XmlPath(“.”)
@XmlJavaTypeAdapter(AlphAdapter.class)
私有LinkedHashMap gLinkedHashMap=新建
LinkedHashMap();
受保护的void put(字符串theKey,字符串theValue){
cLinkedHashMap.put(键,值);
gLinkedHashMap.put(键,值);
}
受保护的字符串获取(字符串密钥){
return(String)cLinkedHashMap.get(theKey);
}
受保护集getCEntrySet(){
返回cLinkedHashMap.entrySet();
}
受保护集getGEntrySet(){
返回gLinkedHashMap.entrySet();
}
受保护的LinkedHashMap getCLinkedHashMap(){
返回cLinkedHashMap;
}
受保护的LinkedHashMap getGLinkedHashMap(){
返回gLinkedHashMap;
}
公共字符串toCXML()引发XMLHandlingException{
返回null;
}
公共字符串toGXML()引发XMLHandlingException{
返回null;
}
}
@XmlRootElement(name=“AlphReq”)
@XmlDiscriminatorValue(“AlphabetReq”)
@XmlAccessorType(XmlAccessType.FIELD)
公共类AlphabetReq扩展了Alph{
公共静态AlphabetReq getInstance(字符串theAlphabetReqXML)抛出
XMLHandlingException{
返回XMLUtils.parseAlphabetReqXML(theAlphabetReqXML);
}
公共字符串toCXML()引发XMLHandlingException{
返回XMLUtils.getAlphabetReqXML(this);
}
}
@XmlRootElement(name=“AlphResp”)
@XmlDiscriminatorValue(“AlphabetResp”)
@XmlAccessorType(XmlAccessType.FIELD)
公共类AlphabetResp扩展了Alph{
公共静态AlphabetResp getInstance(字符串theAlphabetRespXML)抛出
XMLHandlingException{
返回XMLUtils.parseAlphabetRespXML(theAlphabetRespXML);
}
公共字符串toCXML()引发XMLHandlingException{
返回XMLUtils.getAlphabetRespXML(this);
}
}
我创建了以下用于编组和解编组的XMLUtil方法

public static String getAlphabetReqXML(Alph theAlphabet) throws XMLHandlingException {

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Writer writer = null;

try {
    writer = new OutputStreamWriter(byteArrayOutputStream, "UTF-8");
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}

try {
    writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" );
} catch (IOException e) {
    e.printStackTrace();
}

try {
    JAXBContext JContext = JAXBContext.newInstance(AlphabetReq.class); 
    Marshaller JMarshaller = JContext.createMarshaller();
    JMarshaller.marshal(theAlphabet, writer);
} catch (Throwable e) {
    e.printStackTrace();
}

String theAlphabetReqXML = byteArrayOutputStream.toString();
return theAlphabetReqXML;

}

public static AlphabetReq parseAlphabetReqXML(String theAlphabetReqXML) throws 
XMLHandlingException {

if(null == theAlphabetReqXML) {
    return null;
}
try {
    InputStream IPStream = new ByteArrayInputStream(theAlphabetReqXML.getBytes());
    JAXBContext JContext = JAXBContext.newInstance(AlphabetReq.class);
    Unmarshaller JUnmarshaller = JContext.createUnmarshaller();
    AlphabetReq alphabetreq = (AlphabetReq) JUnmarshaller.unmarshal(IPStream);
    return alphabetreq;
} catch(Throwable t) {
    t.printStackTrace();
}
}

public static String getAlphabetRespXML(Alph theAlphabet) throws XMLHandlingException {

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Writer writer = null;

try {
    writer = new OutputStreamWriter(byteArrayOutputStream, "UTF-8");
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}

try {
    writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" );
} catch (IOException e) {
    e.printStackTrace();
}

try {
    JAXBContext JContext = JAXBContext.newInstance(AlphabetResp.class); 
    Marshaller JMarshaller = JContext.createMarshaller();
    JMarshaller.marshal(theAlphabet, writer);
} catch (Throwable e) {
    e.printStackTrace();
}

String theAlphabetRespXML = byteArrayOutputStream.toString();
return theAlphabetRespXML;

}

public static AlphabetResp parseAlphabetReqXML(String theAlphabetRespXML) throws 
XMLHandlingException {

if(null == theAlphabetRespXML) {
    return null;
}
try {
    InputStream IPStream = new ByteArrayInputStream(theAlphabetRespXML.getBytes());
    JAXBContext JContext = JAXBContext.newInstance(AlphabetResp.class);
    Unmarshaller JUnmarshaller = JContext.createUnmarshaller();
    AlphabetResp alphabetresp = (AlphabetResp) JUnmarshaller.unmarshal(IPStream);
    return alphabetresp;
} catch(Throwable t) {
    t.printStackTrace();
}
}
公共静态字符串getAlphabetReqXML(Alph theAlphabet)抛出XMLHandlingException{
ByteArrayOutputStream ByteArrayOutputStream=新建ByteArrayOutputStream();
Writer=null;
试一试{
writer=新的输出流writer(byteArrayOutputStream,“UTF-8”);
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
试一试{
作者:写(“”);
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
JAXBContext JContext=JAXBContext.newInstance(AlphabetReq.class);
Marshaller JMarshaller=JContext.createMarshaller();
马歇尔(theAlphabet,作家);
}捕获(可丢弃的e){
e、 printStackTrace();
}
字符串theAlphabetReqXML=byteArrayOutputStream.toString();
返回alphabetreqxml;
}
公共静态AlphabetReq parseAlphabetReqXML(字符串theAlphabetReqXML)抛出
XMLHandlingException{
if(null==theAlphabetReqXML){
返回null;
}
试一试{
InputStream IPStream=new ByteArrayInputStream(theAlphabetReqXML.getBytes());
JAXBContext JContext=JAXBContext.newInstance(AlphabetReq.class);
Unmarshaller JUnmarshaller=JContext.createUnmarshaller();
AlphabetReq AlphabetReq=(AlphabetReq)JUnmarshaller.unmarshal(IPStream);
返回alphabetreq;
}捕获(可丢弃的t){
t、 printStackTrace();
}
}
公共静态字符串getAlphabetRespXML(Alph theAlphabet)抛出XMLHandlingException{
ByteArrayOutputStream ByteArrayOutputStream=新建ByteArrayOutputStream();
Writer=null;
试一试{
writer=新的输出流writer(byteArrayOutputStream,“UTF-8”);
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
试一试{
作者:写(“”);
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
JAXBContext JContext=JAXBContext.newInstance(AlphabetResp.class);
Marshaller JMarshaller=JContext.createMarshaller();
马歇尔(theAlphabet,作家);
}捕获(可丢弃的e){
e、 printStackTrace();
}
字符串theAlphabetRespXML=byteArrayOutputStream.toString();
返回alphabetrespXML;
}
公共静态AlphabetResp parseAlphabetReqXML(字符串theAlphabetRespXML)抛出
XMLHandlingException{
if(null==theAlphabetRespXML){
返回null;
}
试一试{
InputStream IPStream=新的ByteArrayInputStream(theAlphabetRespXML.getBytes());
JAXBContext JContext=JAXBContext.newInstance(AlphabetResp.class);
Unmarshaller JUnmarshaller=JContext.createUnmarshaller();
AlphabetResp AlphabetResp=(AlphabetResp)JUnmarshaller.unmarshal(IPStream);
返回alphabetresp;
}捕获(可丢弃的t){
t、 printStackTrace();
}
}
并引入了一个适配器类

import java.util.*;
import java.util.Map.Entry;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.eclipse.persistence.oxm.annotations.XmlVariableNode;

public class AlphAdapter extends XmlAdapter<AlphAdapter.AdaptedMap, LinkedHashMap<String, String>>{

public static class AdaptedMap {
    @XmlVariableNode("key")
    List<AdaptedEntry> entries = new ArrayList<AdaptedEntry>();  
}

public static class AdaptedEntry {  
    @XmlTransient
    public String key;

    @XmlValue
    public String value;
}

@Override
public AdaptedMap marshal(LinkedHashMap<String, String> map) throws Exception {
    AdaptedMap adaptedMap = new AdaptedMap();
    for(Entry<String, String> entry : map.entrySet()) {
        AdaptedEntry adaptedEntry = new AdaptedEntry();
        adaptedEntry.key = entry.getKey();
        adaptedEntry.value = entry.getValue();
        adaptedMap.entries.add(adaptedEntry);
    }
    return adaptedMap;
}

@Override
public LinkedHashMap<String, String> unmarshal(AdaptedMap adaptedMap) throws Exception {
    List<AdaptedEntry> adaptedEntries = adaptedMap.entries;
    LinkedHashMap<String, String> map = new LinkedHashMap<String, String>(adaptedEntries.size());
    for(AdaptedEntry adaptedEntry : adaptedMap.entries) {
        map.put(adaptedEntry.key, adaptedEntry.value);
    }
    return map;
}
}
import java.util.*;
导入java.util.Map.Entry;
导入javax.xml.bind.annotation.*;
导入javax.xml.bind.annotation.adapters.XmlAdapter;
导入org.eclipse.persistence.oxm.annotations.XmlVariableNode;
公共类AlphaAdapter扩展了XmlAdapter{
公共静态类AdaptedMap{
@XmlVariableNode(“键”)
列表项=新的ArrayList();
}
公共静态类AdaptedEntry{
@XmlTransient
公共字符串密钥;
@XmlValue
公共字符串值;
}
@凌驾
公共AdaptedMap封送处理(LinkedHashMap映射)引发异常{
AdaptedMap AdaptedMap=新的AdaptedMap();
for(条目:map.entrySet()){
AdaptedEntry AdaptedEntry=新AdaptedEntry();
adaptedEntry.key=entry.getKey();
adaptedEntry.value=entry.getValue();
adaptedMap.entries.add(adaptedEntry);
}
返回自适应地图;
}
@凌驾
公共LinkedHashMap解组(AdaptedMap AdaptedMap)引发异常{
列表adaptedEntries=AdaptedEdm
@XmlAccessorType(XmlAccessType.FIELD)
public class AlphabetReq 
{
   @XmlAnyElement
   private List<Element> allTags = new ArrayList<Element>();

   List<Element> getAllTags()
   {
      return allTags;
   }

   void setAllTags(List<Element> allTags)
   {
      this.allTags = allTags;
   }

   public Map<String, String> getValues()
   {
      final Map<String, String> retVal = new HashMap<String, String>();
      for (Element el : allTags)
      {
         retVal.put(el.getNodeName(), el.getTextContent());
      }
      return retVal;
   }
}  
{f=Fox, d=Dog, e=Elephant, b=Ball, c=Cat, a=Apple}  
  AlphabetReq obj = JAXB.unmarshal(new java.io.File("D:/test.xml"), AlphabetReq.class);
  System.out.println(obj.getValues());
  JAXB.marshal(obj, System.out);
@XmlPath(".")
@XmlJavaTypeAdapter(AlphAdapter.class)
private LinkedHashMap<String, String> cLinkedHashMap = new 
        LinkedHashMap<String, String>();

@XmlPath(".")
@XmlJavaTypeAdapter(AlphAdapter.class)
private LinkedHashMap<String, String> gLinkedHashMap = new 
        LinkedHashMap<String, String>();
import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(AlphabetReq.class, AlphabetResp.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum18741690/input.xml");
        AlphabetReq ar = (AlphabetReq) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(ar, System.out);
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<AlphReq xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="AlphabetReq">
   <a/>
   <b/>
   <c/>
   <d/>
   <e/>
   <f/>
   <a>Apple</a>
   <b>Ball</b>
   <c>Cat</c>
   <d>Dog</d>
   <e>Elephant</e>
   <f>Fox</f>
</AlphReq>