簡體   English   中英

在 Java 中獲取對象引用名稱以構建 XML

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

我正在使用 XML 生成器編寫自定義 XML 編組器。 我堅持寫標簽名稱。

例子:

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

我的要求是:

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

您可以使用 Jaxb 實現這一點。 請在實現之前將內部類提取到文件中。

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();
        }
    }
}

這將輸出:

<?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