Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Self-closing tag, xml to pojo fail #27

Open
wadechen2003 opened this issue Jan 11, 2017 · 4 comments
Open

Self-closing tag, xml to pojo fail #27

wadechen2003 opened this issue Jan 11, 2017 · 4 comments

Comments

@wadechen2003
Copy link

wadechen2003 commented Jan 11, 2017

Hi , I tried to convert xml to a java object
if I put a self-closing tag like < attribute />
the converter went wrong.
is this supported in gson-xml ?
thanks !

@roman-mazur
Copy link
Contributor

Could you give a specific example: XML input + how you initialize the parser?

@frog1014
Copy link

frog1014 commented Jan 11, 2017

I met the same issue.
Any attributes under the first close tag(here is an array <action_list/> will be converted to be 0 or empty.
public GsonXml parser() {

    XmlParserCreator parserCreator = new XmlParserCreator() {
        @Override
        public XmlPullParser createParser() {
            try {
                return XmlPullParserFactory.newInstance().newPullParser();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    GsonXml gsonXml = new GsonXmlBuilder()
            .setXmlParserCreator(parserCreator)
            .create();
    return gsonXml;
}

GsonXml gsonXml = CMSResXmlObject.parser();
AutomationRuleBean ret = parser.fromXml(xmlString, AutomationRuleBean.class);
List<AutomationRuleBean> list = ret.getAutomationList();

the XML input:

<?xml version="1.0" encoding="UTF-8"?> <root> <status>0</status> <automation_list> <automation> <automation_id>6</automation_id> <name>LIVING_LIGHT</name> <mode>1</mode> <schedule></schedule> <delay>0</delay> <arm_state>1</arm_state> <rf_id>16</rf_id> <camera_mac /> <feature_name>state</feature_name> <value>ON</value> <action_list> <action> <action_type>0</action_type> <camera_mac></camera_mac> <rf_id>13</rf_id> <feature_name>state</feature_name> <value>255.0</value> </action> <action> <action_type>0</action_type> <camera_mac></camera_mac> <rf_id>14</rf_id> <feature_name>state</feature_name> <value>255.0</value> </action> </action_list> </automation> <automation> <automation_id>7</automation_id> <name>to h</name> <mode>1</mode> <schedule>1</schedule> <delay>3661</delay> <arm_state>1</arm_state> <rf_id>1</rf_id> <camera_mac /> <feature_name>state</feature_name> <value>ON</value> <action_list /> </automation> <automation> <automation_id>9</automation_id> <name>to</name> <mode>1</mode> <schedule>1</schedule> <delay>3661</delay> <arm_state>1</arm_state> <rf_id>1</rf_id> <camera_mac /> <feature_name>state</feature_name> <value>ON</value> <action_list /> </automation> </automation_list> </root>

@boomsya
Copy link

boomsya commented May 30, 2017

does someone have solutions?

@javadev
Copy link

javadev commented Oct 8, 2018

@boomsya You may try to convert xml string to json string with help of underscore-java library.

Gson gson = new Gson();
Staff staff = gson.fromJson(U.xmlToJson(xmlString), Staff.class);

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants