zoukankan      html  css  js  c++  java
  • JAXB example.

    package com.karl.jaxb;

    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;

    @XmlRootElement(name = "abc", namespace = "http://www.karl.com")
    @XmlAccessorType(XmlAccessType.PROPERTY)
    public class Star {
        private String name = "Lady Gaga";
        @XmlElement
        int age = 10;
        @XmlAttribute
        String gender = "female";

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }


    package com.karl.jaxb;
    import java.io.StringReader;

    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBException;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;

    public class TestJAXB {
        public static void main(String[] args) throws JAXBException {
            JAXBContext context = JAXBContext.newInstance(Star.class);

            Marshaller marshaller = context.createMarshaller();

            Unmarshaller unmarsheller = context.createUnmarshaller();

            Star star = new Star();

            marshaller.marshal(star, System.out);

            System.out.println();

            String xml = "<abc xmlns=\"http://www.karl.com\"><name>Adele</name></abc>";

            Star star2 = (Star) unmarsheller.unmarshal(new StringReader(xml));

            System.out.println(star2.getName());
        }
    }
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns2:abc xmlns:ns2="http://www.karl.com" gender="female"><age>10</age><name>Lady Gaga</name></ns2:abc>

    Lady Gaga

  • 相关阅读:
    3D标签云
    IntelliJ IDEA 13.1.1版本偶然的错误
    414. Third Maximum Number
    217. Contains Duplicate
    442.Find All Duplicates in an Array
    3D轮播图
    448. Find All Numbers Disappeared in an Array
    Beautifulsoup模块
    MySQL数据库
    常用模块
  • 原文地址:https://www.cnblogs.com/zhonghan/p/2631798.html
Copyright © 2011-2022 走看看