简体   繁体   English

在 Java 中获取对象引用名称以构建 XML

[英]Get object reference name in Java for constructing XML

I am writing custom XML marshaller using XML generators.我正在使用 XML 生成器编写自定义 XML 编组器。 I am stuck with tag names writing.我坚持写标签名称。

Example:例子:

     List<String> userList = new ArrayList<String>();
      userList.add("UserA");
      userList.add("UserB");
     Map<String, Object> systemMap = new HashMap<String, Object>();
     systemMap.put("SystemA",userList);

My requirement is:我的要求是:

      <SystemA>
          <userList> 
             [userA,userB]
          </userList>
      </SystemA>

You can achieve that using Jaxb.您可以使用 Jaxb 实现这一点。 Please extract inner classes to file before implementing.请在实现之前将内部类提取到文件中。

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.io.StringWriter;
import java.util.*;

public class ParsingTest {

    private JAXBContext jaxbContext;
    private Marshaller marshaller;

    @Before
    public void setUp() throws Exception {
        jaxbContext = JAXBContext.newInstance(SystemA.class);
        marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    }

    @Test
    public void testParse() throws Exception {
        final SystemA systemA = new SystemA();
        final List<String> users = new ArrayList<>();
        users.add("userA");
        users.add("userB");
        systemA.setUserList(users);

        final StringWriter stringWriter = new StringWriter();
        marshaller.marshal(systemA, stringWriter);
        System.out.println(stringWriter.toString());
        Assert.assertTrue(true);

        // XMLUnit.compareXML(stringWriter.toString(), reader);

    }

    @XmlRootElement(name = "SystemA")
    @XmlType(name = "SystemA", propOrder = {
            "userList"
    })
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class SystemA {

        @XmlJavaTypeAdapter(CustomAdapter.class)
        private Collection<String> userList;

        public Collection<String> getUserList() {
            return userList;
        }

        public void setUserList(Collection<String> userList) {
            this.userList = userList;
        }
    }

    public static class CustomAdapter extends XmlAdapter<String, Collection<String>> {

        @Override
        public Collection<String> unmarshal(String v) throws Exception {
            return new ArrayList<>();// TODO String -> List;
        }

        @Override
        public String marshal(Collection<String> v) throws Exception {
            final StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("[");
            for (String s : v) {
                stringBuilder.append(s);
                stringBuilder.append(",");
            }

            stringBuilder.deleteCharAt(stringBuilder.lastIndexOf(","));
            stringBuilder.append("]");
            return stringBuilder.toString();
        }
    }
}

This will output:这将输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SystemA>
    <userList>[userA,userB]</userList>
</SystemA>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM