簡單動態(tài)XML

2018-12-31 21:01 更新

關于簡單動態(tài)XML

有一點需要注意,之前所有指定的字段都指定了數(shù)據(jù),但是如果不指定數(shù)據(jù),就能生成不一樣的XML

  1. @Test
  2. public void test4() throws JAXBException {
  3. Product p1 = new Product();
  4. p1.setId("11041");
  5. Product p2 = new Product();
  6. p2.setId("11042");
  7. p2.setName("Grape");
  8. List<Product> list = Arrays.asList(p1,p2);
  9. Order3 order = new Order3();
  10. order.setId("1104");
  11. order.setProduct(list);
  12. JAXBContext context = JAXBContext.newInstance(Order3.class);
  13. Marshaller marshaller = context.createMarshaller();
  14. marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  15. marshaller.marshal(order, System.out);
  16. }

生成的XML只包含上面指定數(shù)據(jù)的字段。

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2. <Order>
  3. <id>1104</id>
  4. <Products>
  5. <product id="11041"/>
  6. <product id="11042">
  7. <name>Grape</name>
  8. </product>
  9. </Products>
  10. </Order>

動態(tài) XML Element

有時候,Products元素下面并不只是加入 product ,可能動態(tài)加入這種商品。 這次的模擬的商品是糕點與餅干。

這里的 Cake 只包含一個字段,需要注意的是 @XmlRootElement不能少。

  1. @XmlRootElement(name = "Cake")
  2. public class Cake {
  3. private String name;
  4. //setters, getters
  5. }

這里的 Biscuit 也只包含一個字段。

  1. @XmlRootElement(name = "Biscuit")
  2. public class Biscuit {
  3. private String name;
  4. //setters, getters
  5. }

Order的第三個字段是List,但是沒有指定一個特定對象,用了Object代指所有,還有一個@XmlAnyElement是最重要的注解,用來標注所有的Element。

  1. @XmlRootElement(name = "Order")
  2. @XmlAccessorType(XmlAccessType.FIELD)
  3. public class Order4 {
  4. private String id;
  5. private Double price;
  6. @XmlElementWrapper(name = "Products")
  7. @XmlAnyElement
  8. private List<Object> product;
  9. //setters, getters
  10. }

下面用來模擬數(shù)據(jù)生成,注意的是JAXBContext需要注冊所有需要編組的Java bean。這一點和之前的例子是不同的。

  1. @Test
  2. public void test4() throws JAXBException {
  3. Cake cake = new Cake();
  4. cake.setName("Nobel");
  5. Biscuit biscuit = new Biscuit();
  6. biscuit.setName("PB");
  7. List<Object> list = Arrays.asList(cake,biscuit);
  8. Order4 order = new Order4();
  9. order.setId("1104");
  10. order.setProduct(list);
  11. JAXBContext context = JAXBContext.newInstance(Order4.class, Cake.class, Biscuit.class);
  12. Marshaller marshaller = context.createMarshaller();
  13. marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  14. marshaller.marshal(order, System.out);
  15. }

生成的XML包含了一個 Products ,其數(shù)據(jù)結構在Order中并沒有指定,方便后期擴展。

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2. <Order>
  3. <id>1104</id>
  4. <Products>
  5. <Cake>
  6. <name>Nobel</name>
  7. </Cake>
  8. <Biscuit>
  9. <name>PB</name>
  10. </Biscuit>
  11. </Products>
  12. </Order>

XML 動態(tài) Attribute

與動態(tài)Element對應的是Attribute,不過需要注意的是,動態(tài) Attribute 需要的是 Map ,而且其 key 的類型需要指定為 QName,這個QName在復雜的 XML 生成時有很大用處。一般使用到的是其QName(name),它還有一個形式為QName(name,namespace),可以指定命名空間,在某些場景下有不可替代的作用。

  1. @XmlRootElement(name = "Order")
  2. @XmlAccessorType(XmlAccessType.FIELD)
  3. public class Order5 {
  4. @XmlAnyAttribute
  5. private Map<QName, String> properties;
  6. private Product product;
  7. //setters, getters
  8. }

  1. @Test
  2. public void test5() throws JAXBException {
  3. Product p = new Product();
  4. p.setId("1100");
  5. p.setName("Apple");
  6. Map<QName, String> map = new HashMap<>();
  7. map.put(new QName("id"), "1105");
  8. map.put(new QName("classification"), "food");
  9. map.put(new QName("type"), "fruit");
  10. Order5 order = new Order5();
  11. order.setProduct(p);
  12. order.setProperties(map);
  13. JAXBContext context = JAXBContext.newInstance(Order5.class);
  14. Marshaller marshaller = context.createMarshaller();
  15. marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  16. marshaller.marshal(order, System.out);
  17. }

僅關注 Order 的 Attribute , 可以發(fā)現(xiàn)我們在 Order 中沒有指定任何與之相關的字段,只是在HashMap中加了幾組數(shù)據(jù),現(xiàn)在編組成為了Attribute。

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2. <Order id="1105" classification="food" type="fruit">
  3. <product id="1100">
  4. <name>Apple</name>
  5. </product>
  6. </Order>
以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號