美文网首页Java web
解析xml数据转化成对象(JAXB)

解析xml数据转化成对象(JAXB)

作者: 蛋皮皮652 | 来源:发表于2019-11-08 14:02 被阅读0次

    jaxb

    主要代码截图

    实体类

    xmlUtils

    package com.jpxx.homepage.homePage.web.utils;

    import javax.xml.bind.JAXBContext;

    import javax.xml.bind.Unmarshaller;

    import java.io.StringReader;

    public class XmlUtils {

        public static ObjectconvertXmlStrToObject(Class clazz,String xmlStr)throws Exception{

    JAXBContext context= JAXBContext.newInstance(clazz);

            Unmarshaller unmarshaller=context.createUnmarshaller();

            StringReader sr=new StringReader(xmlStr);

            return unmarshaller.unmarshal(sr);

        }

    /**将土壤墒情特征接口xmlstring转成对象

        *xmlString转换成对象

    */

        public static ObjectconvertSoilCharacterXmlStrToObject(Class clazz,String xmlStr)throws Exception{

    JAXBContext context= JAXBContext.newInstance(clazz);

            Unmarshaller unmarshaller=context.createUnmarshaller();

            StringReader sr=new StringReader(xmlStr);

            return unmarshaller.unmarshal(sr);

        }

    public static void main(String[] args) {

    //xml字符串

    <Entries>

    <Entry>

        <FadeMoisture50cm>5.7</FadeMoisture50cm>

        <SoilWeight90cm>1.2</SoilWeight90cm>

        <FadeMoisture30cm>5.7</FadeMoisture30cm>

        <SoilWeight50cm>1.2</SoilWeight50cm>

        <SoilWeight30cm>1.2</SoilWeight30cm>

        <StationID>410102B01</StationID>

        <SoilWeight70cm>1.2</SoilWeight70cm>

        <FieldCapacity80cm>30.1</FieldCapacity80cm>

        <FieldCapacity100cm>30.1</FieldCapacity100cm>

        <FieldCapacity40cm>30.1</FieldCapacity40cm>

        <SoilWeight100cm>1.2</SoilWeight100cm>

        <FieldCapacity60cm>30.1</FieldCapacity60cm>

        <FadeMoisture80cm>5.7</FadeMoisture80cm>

        <FieldCapacity20cm>30.1</FieldCapacity20cm>

        <FadeMoisture60cm>5.7</FadeMoisture60cm>

        <FadeMoisture100cm>5.7</FadeMoisture100cm>

        <FadeMoisture40cm>5.7</FadeMoisture40cm>

        <FadeMoisture20cm>5.7</FadeMoisture20cm>

        <SoilType>半水成土</SoilType>

        <SoilWeight40cm>1.2</SoilWeight40cm>

        <SoilWeight20cm>1.2</SoilWeight20cm>

        <SoilWeight60cm>1.2</SoilWeight60cm>

        <SoilWeight80cm>1.2</SoilWeight80cm>

        <FieldCapacity90cm>30.1</FieldCapacity90cm>

        <FieldCapacity70cm>30.1</FieldCapacity70cm>

        <SoilWeight10cm>1.2</SoilWeight10cm>

        <SoilTexture>砂质粘壤土</SoilTexture>

        <FieldCapacity30cm>30.1</FieldCapacity30cm>

        <FadeMoisture10cm>5.7</FadeMoisture10cm>

        <FieldCapacity50cm>30.1</FieldCapacity50cm>

        <FieldCapacity10cm>30.1</FieldCapacity10cm>

        <FadeMoisture70cm>5.7</FadeMoisture70cm>

        <FadeMoisture90cm>5.7</FadeMoisture90cm>

    </Entry>

    </Entries>

            String xmlData ="<Entries>\n" +

    "<Entry>\n" +

    "    <FadeMoisture50cm>5.7</FadeMoisture50cm>\n" +

    "    <SoilWeight90cm>1.2</SoilWeight90cm>\n" +

    "    <FadeMoisture30cm>5.7</FadeMoisture30cm>\n" +

    "    <SoilWeight50cm>1.2</SoilWeight50cm>\n" +

    "    <SoilWeight30cm>1.2</SoilWeight30cm>\n" +

    "    <StationID>410102B01</StationID>\n" +

    "    <SoilWeight70cm>1.2</SoilWeight70cm>\n" +

    "    <FieldCapacity80cm>30.1</FieldCapacity80cm>\n" +

    "    <FieldCapacity100cm>30.1</FieldCapacity100cm>\n" +

    "    <FieldCapacity40cm>30.1</FieldCapacity40cm>\n" +

    "    <SoilWeight100cm>1.2</SoilWeight100cm>\n" +

    "    <FieldCapacity60cm>30.1</FieldCapacity60cm>\n" +

    "    <FadeMoisture80cm>5.7</FadeMoisture80cm>\n" +

    "    <FieldCapacity20cm>30.1</FieldCapacity20cm>\n" +

    "    <FadeMoisture60cm>5.7</FadeMoisture60cm>\n" +

    "    <FadeMoisture100cm>5.7</FadeMoisture100cm>\n" +

    "    <FadeMoisture40cm>5.7</FadeMoisture40cm>\n" +

    "    <FadeMoisture20cm>5.7</FadeMoisture20cm>\n" +

    "    <SoilType>半水成土</SoilType>\n" +

    "    <SoilWeight40cm>1.2</SoilWeight40cm>\n" +

    "    <SoilWeight20cm>1.2</SoilWeight20cm>\n" +

    "    <SoilWeight60cm>1.2</SoilWeight60cm>\n" +

    "    <SoilWeight80cm>1.2</SoilWeight80cm>\n" +

    "    <FieldCapacity90cm>30.1</FieldCapacity90cm>\n" +

    "    <FieldCapacity70cm>30.1</FieldCapacity70cm>\n" +

    "    <SoilWeight10cm>1.2</SoilWeight10cm>\n" +

    "    <SoilTexture>砂质粘壤土</SoilTexture>\n" +

    "    <FieldCapacity30cm>30.1</FieldCapacity30cm>\n" +

    "    <FadeMoisture10cm>5.7</FadeMoisture10cm>\n" +

    "    <FieldCapacity50cm>30.1</FieldCapacity50cm>\n" +

    "    <FieldCapacity10cm>30.1</FieldCapacity10cm>\n" +

    "    <FadeMoisture70cm>5.7</FadeMoisture70cm>\n" +

    "    <FadeMoisture90cm>5.7</FadeMoisture90cm>\n" +

    "</Entry>\n" +

    "</Entries>";

            try {

    SoilCharacterBean soilCharacterBean = (SoilCharacterBean) XmlUtils.convertSoilCharacterXmlStrToObject(SoilCharacterBean.class,xmlData);

                System.out.println("zzzz "+soilCharacterBean.getEntry().getFadeMoisture10cm());

            }catch (Exception e) {

    e.printStackTrace();

            }

    }

    }

    实体类

    package com.jpxx.homepage.homePage.web.utils;

    import javax.xml.bind.annotation.XmlElement;

    import javax.xml.bind.annotation.XmlRootElement;

    import javax.xml.bind.annotation.XmlType;

    @XmlRootElement(name="Entries")

    @XmlType(propOrder={"entry"})

    public class SoilCharacterBean {

    private Entryentry;

        @XmlElement(name="Entry")

    public EntrygetEntry() {

    return entry;

        }

    public void setEntry(Entry entry) {

    this.entry = entry;

        }

    public static class Entry{

    private StringFadeMoisture50cm;

            private StringSoilWeight90cm;

            private StringFadeMoisture30cm;

            private StringSoilWeight50cm;

            private StringSoilWeight30cm;

            private StringStationID;

            private StringSoilWeight70cm;

            private StringFieldCapacity80cm;

            private StringFieldCapacity100cm;

            private StringFieldCapacity40cm;

            private StringSoilWeight100cm;

            private StringFieldCapacity60cm;

            private StringFadeMoisture80cm;

            private StringFieldCapacity20cm;

            private StringFadeMoisture60cm;

            private StringFadeMoisture100cm;

            private StringFadeMoisture40cm;

            private StringFadeMoisture20cm;

            private StringSoilType;

            private StringSoilWeight40cm;

            private StringSoilWeight20cm;

            private StringSoilWeight60cm;

            private StringSoilWeight80cm;

            private StringFieldCapacity90cm;

            private StringFieldCapacity70cm;

            private StringSoilWeight10cm;

            private StringSoilTexture;

            private StringFieldCapacity30cm;

            private StringFadeMoisture10cm;

            private StringFieldCapacity50cm;

            private StringFieldCapacity10cm;

            private StringFadeMoisture70cm;

            private StringFadeMoisture90cm;

            @XmlElement(name="FadeMoisture50cm")

    public StringgetFadeMoisture50cm() {

    return FadeMoisture50cm;

            }

    public void setFadeMoisture50cm(String fadeMoisture50cm) {

    FadeMoisture50cm = fadeMoisture50cm;

            }

    @XmlElement(name="SoilWeight90cm")

    public StringgetSoilWeight90cm() {

    return SoilWeight90cm;

            }

    public void setSoilWeight90cm(String soilWeight90cm) {

    SoilWeight90cm = soilWeight90cm;

            }

    @XmlElement(name="FadeMoisture30cm")

    public StringgetFadeMoisture30cm() {

    return FadeMoisture30cm;

            }

    public void setFadeMoisture30cm(String fadeMoisture30cm) {

    FadeMoisture30cm = fadeMoisture30cm;

            }

    @XmlElement(name="SoilWeight50cm")

    public StringgetSoilWeight50cm() {

    return SoilWeight50cm;

            }

    public void setSoilWeight50cm(String soilWeight50cm) {

    SoilWeight50cm = soilWeight50cm;

            }

    @XmlElement(name="SoilWeight30cm")

    public StringgetSoilWeight30cm() {

    return SoilWeight30cm;

            }

    public void setSoilWeight30cm(String soilWeight30cm) {

    SoilWeight30cm = soilWeight30cm;

            }

    @XmlElement(name="StationID")

    public StringgetStationID() {

    return StationID;

            }

    public void setStationID(String stationID) {

    StationID = stationID;

            }

    @XmlElement(name="SoilWeight70cm")

    public StringgetSoilWeight70cm() {

    return SoilWeight70cm;

            }

    public void setSoilWeight70cm(String soilWeight70cm) {

    SoilWeight70cm = soilWeight70cm;

            }

    @XmlElement(name="FieldCapacity80cm")

    public StringgetFieldCapacity80cm() {

    return FieldCapacity80cm;

            }

    public void setFieldCapacity80cm(String fieldCapacity80cm) {

    FieldCapacity80cm = fieldCapacity80cm;

            }

    @XmlElement(name="FieldCapacity100cm")

    public StringgetFieldCapacity100cm() {

    return FieldCapacity100cm;

            }

    public void setFieldCapacity100cm(String fieldCapacity100cm) {

    FieldCapacity100cm = fieldCapacity100cm;

            }

    @XmlElement(name="FieldCapacity40cm")

    public StringgetFieldCapacity40cm() {

    return FieldCapacity40cm;

            }

    public void setFieldCapacity40cm(String fieldCapacity40cm) {

    FieldCapacity40cm = fieldCapacity40cm;

            }

    @XmlElement(name="SoilWeight100cm")

    public StringgetSoilWeight100cm() {

    return SoilWeight100cm;

            }

    public void setSoilWeight100cm(String soilWeight100cm) {

    SoilWeight100cm = soilWeight100cm;

            }

    @XmlElement(name="FieldCapacity60cm")

    public StringgetFieldCapacity60cm() {

    return FieldCapacity60cm;

            }

    public void setFieldCapacity60cm(String fieldCapacity60cm) {

    FieldCapacity60cm = fieldCapacity60cm;

            }

    @XmlElement(name="FadeMoisture80cm")

    public StringgetFadeMoisture80cm() {

    return FadeMoisture80cm;

            }

    public void setFadeMoisture80cm(String fadeMoisture80cm) {

    FadeMoisture80cm = fadeMoisture80cm;

            }

    @XmlElement(name="FieldCapacity20cm")

    public StringgetFieldCapacity20cm() {

    return FieldCapacity20cm;

            }

    public void setFieldCapacity20cm(String fieldCapacity20cm) {

    FieldCapacity20cm = fieldCapacity20cm;

            }

    @XmlElement(name="FadeMoisture60cm")

    public StringgetFadeMoisture60cm() {

    return FadeMoisture60cm;

            }

    public void setFadeMoisture60cm(String fadeMoisture60cm) {

    FadeMoisture60cm = fadeMoisture60cm;

            }

    @XmlElement(name="FadeMoisture100cm")

    public StringgetFadeMoisture100cm() {

    return FadeMoisture100cm;

            }

    public void setFadeMoisture100cm(String fadeMoisture100cm) {

    FadeMoisture100cm = fadeMoisture100cm;

            }

    @XmlElement(name="FadeMoisture40cm")

    public StringgetFadeMoisture40cm() {

    return FadeMoisture40cm;

            }

    public void setFadeMoisture40cm(String fadeMoisture40cm) {

    FadeMoisture40cm = fadeMoisture40cm;

            }

    @XmlElement(name="FadeMoisture20cm")

    public StringgetFadeMoisture20cm() {

    return FadeMoisture20cm;

            }

    public void setFadeMoisture20cm(String fadeMoisture20cm) {

    FadeMoisture20cm = fadeMoisture20cm;

            }

    @XmlElement(name="SoilType")

    public StringgetSoilType() {

    return SoilType;

            }

    public void setSoilType(String soilType) {

    SoilType = soilType;

            }

    @XmlElement(name="SoilWeight40cm")

    public StringgetSoilWeight40cm() {

    return SoilWeight40cm;

            }

    public void setSoilWeight40cm(String soilWeight40cm) {

    SoilWeight40cm = soilWeight40cm;

            }

    @XmlElement(name="SoilWeight20cm")

    public StringgetSoilWeight20cm() {

    return SoilWeight20cm;

            }

    public void setSoilWeight20cm(String soilWeight20cm) {

    SoilWeight20cm = soilWeight20cm;

            }

    @XmlElement(name="SoilWeight60cm")

    public StringgetSoilWeight60cm() {

    return SoilWeight60cm;

            }

    public void setSoilWeight60cm(String soilWeight60cm) {

    SoilWeight60cm = soilWeight60cm;

            }

    @XmlElement(name="SoilWeight80cm")

    public StringgetSoilWeight80cm() {

    return SoilWeight80cm;

            }

    public void setSoilWeight80cm(String soilWeight80cm) {

    SoilWeight80cm = soilWeight80cm;

            }

    @XmlElement(name="FieldCapacity90cm")

    public StringgetFieldCapacity90cm() {

    return FieldCapacity90cm;

            }

    public void setFieldCapacity90cm(String fieldCapacity90cm) {

    FieldCapacity90cm = fieldCapacity90cm;

            }

    @XmlElement(name="FieldCapacity70cm")

    public StringgetFieldCapacity70cm() {

    return FieldCapacity70cm;

            }

    public void setFieldCapacity70cm(String fieldCapacity70cm) {

    FieldCapacity70cm = fieldCapacity70cm;

            }

    @XmlElement(name="SoilWeight10cm")

    public StringgetSoilWeight10cm() {

    return SoilWeight10cm;

            }

    public void setSoilWeight10cm(String soilWeight10cm) {

    SoilWeight10cm = soilWeight10cm;

            }

    @XmlElement(name="SoilTexture")

    public StringgetSoilTexture() {

    return SoilTexture;

            }

    public void setSoilTexture(String soilTexture) {

    SoilTexture = soilTexture;

            }

    @XmlElement(name="FieldCapacity30cm")

    public StringgetFieldCapacity30cm() {

    return FieldCapacity30cm;

            }

    public void setFieldCapacity30cm(String fieldCapacity30cm) {

    FieldCapacity30cm = fieldCapacity30cm;

            }

    @XmlElement(name="FadeMoisture10cm")

    public StringgetFadeMoisture10cm() {

    return FadeMoisture10cm;

            }

    public void setFadeMoisture10cm(String fadeMoisture10cm) {

    FadeMoisture10cm = fadeMoisture10cm;

            }

    @XmlElement(name="FieldCapacity50cm")

    public StringgetFieldCapacity50cm() {

    return FieldCapacity50cm;

            }

    public void setFieldCapacity50cm(String fieldCapacity50cm) {

    FieldCapacity50cm = fieldCapacity50cm;

            }

    @XmlElement(name="FieldCapacity10cm")

    public StringgetFieldCapacity10cm() {

    return FieldCapacity10cm;

            }

    public void setFieldCapacity10cm(String fieldCapacity10cm) {

    FieldCapacity10cm = fieldCapacity10cm;

            }

    @XmlElement(name="FadeMoisture70cm")

    public StringgetFadeMoisture70cm() {

    return FadeMoisture70cm;

            }

    public void setFadeMoisture70cm(String fadeMoisture70cm) {

    FadeMoisture70cm = fadeMoisture70cm;

            }

    @XmlElement(name="FadeMoisture90cm")

    public StringgetFadeMoisture90cm() {

    return FadeMoisture90cm;

            }

    public void setFadeMoisture90cm(String fadeMoisture90cm) {

    FadeMoisture90cm = fadeMoisture90cm;

            }

    }

    }

    相关文章

      网友评论

        本文标题:解析xml数据转化成对象(JAXB)

        本文链接:https://www.haomeiwen.com/subject/wzwibctx.html