stanfy / gson-xml

Java library for XML deserialization
Apache License 2.0
200 stars 41 forks source link

Unable to parse array located at 3rd level of nested xml com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: BEGIN_OBJECT expected, but met NAME #29

Open yuviabhi opened 7 years ago

yuviabhi commented 7 years ago

Can't parse <subject> node data. Its giving following error,

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: BEGIN_OBJECT expected, but met NAME
Scopes: INSIDE_OBJECT>NAME>INSIDE_EMBEDDED_ARRAY>INSIDE_OBJECT>NAME>INSIDE_EMBEDDED_ARRAY>INSIDE_OBJECT>NAME>INSIDE_OBJECT
Closed tags: 'semester'/3
Token: null
Tokens queue: BEGIN_OBJECT, NAME, STRING, NAME, STRING, NAME, STRING, null
Values queue: semester, @semno, 2, @min_crd_req, 22, @cum_crd_req, 48, null

POJOs

public class specialisations {    
    @SerializedName("specialisation")
    private List<specialisation> specialisationList;
}

public class specialisation {
    @SerializedName("@dept")
    private String dept = "";
    @SerializedName("@coursecode")
    private String coursecode = "";
    @SerializedName("@coursename")
    private String coursename = "";
    @SerializedName("semester")
    private List<semester> semester;
}

public class semester {    
    @SerializedName("@semno")
    private String semno="";    
    @SerializedName("@min_crd_req")
    private String min_crd_req="";    
    @SerializedName("@cum_crd_req")
    private String cum_crd_req="";    
    @SerializedName("subject")
    private List<subject> subject;
}

public class subject {    
    @SerializedName("subno")
    private String subno="";    
    @SerializedName("subname")
    private String subname="";    
    @SerializedName("ltp")
    private String ltp="";    
    @SerializedName("crd")
    private String crd="";    
    @SerializedName("sub_type")
    private String sub_type="";
}

XML DATA :-

<specialisations>
        <specialisation dept="AT" coursecode="AT1" coursename="EMBEDDED">
                  <semester semno="1"  min_crd_req="26" cum_crd_req="26">
                             <subject>
                                      <subno>1</subno>
                                      <subname>A</subname>
                                      <ltp>1-1</ltp>
                                      <crd>12</crd>
                                      <sub_type>C</sub_type>
                             </subject>
                             <subject>
                              .....same as above........
                             </subject>
                             <subject>
                              .....same as above........
                             </subject>
                  </semester>
                  <semester semno="2"  min_crd_req="26" cum_crd_req="26">
                        .....same as above........
                  </semester>
        </specialisation>

        <specialisation dept="BT" coursecode="BT" coursename="BT">
              .....same as above........
        </specialisation>

        <specialisation dept="CT" coursecode="CT" coursename="CT">
              .....same as above........
        </specialisation>

        <specialisation dept="DT" coursecode="DT" coursename="DT">
              .....same as above........
        </specialisation>

</specialisations>

See error details, POJOs and XML content here : https://drive.google.com/open?id=1Afj66c5ocjpd6E9lK3bJYg_R8XeQzPQlRRpkXJW7BMU

yuviabhi commented 7 years ago

SOLVED myself ! GSON Parser unable to parse my source XML which is like following

                             <subject>
                                      <subno>1</subno>
                                      <subname>A</subname>
                                      <ltp>1-1</ltp>
                                      <crd>12</crd>
                                      <sub_type>C</sub_type>
                             </subject>

So transformed the XML content from element to attributes using the following XSLT file

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
  <xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>  
  <xsl:template match="subject">
    <subject>
      <xsl:for-each select="*">
        <xsl:attribute name="{name()}">
          <xsl:value-of select="text()"/>
        </xsl:attribute>
      </xsl:for-each>
    </subject>
  </xsl:template>  
</xsl:stylesheet>

TRANSFORMED XML

                <subject subno="AE61002" subname="SATELLITE AND INERTIAL NAVIGATION SYSTEMS" ltp="3-1-0" crd="4" sub_type="ELECTIVE- I"/> 
        <subject subno="AE61038" subname="AEROELASTICITY" ltp="3-0-0" crd="3" sub_type="ELECTIVE- I"/> 
        <subject subno="AE61048" subname="COMPOSITE STRUCTURES" ltp="3-0-0" crd="3" sub_type="ELECTIVE- I"/> 
        <subject subno="CE60125" subname="NUMERICAL METHODS IN STRUCTURAL ENGINEERING" ltp="3-0-0" crd="3" sub_type="ELECTIVE- I"/> 
        <subject subno="MA60047" subname="OBJECT ORIENTED PROGRAMMING" ltp="3-1-0" crd="4" sub_type="ELECTIVE- I"/> 
        <subject subno="MA60057" subname="ENGINEERING MATHEMATICS" ltp="3-1-0" crd="4" sub_type="ELECTIVE- I"/> 
        <subject subno="MA60059" subname="ADVANCED NUMERICAL ANALYSIS" ltp="3-1-0" crd="4" sub_type="ELECTIVE- I"/> 
        <subject subno="MA60077" subname="COMPUTER PROGRAMMING" ltp="3-1-0" crd="4" sub_type="ELECTIVE- I"/> 
        <subject subno="AE61005" subname="UNSTEADY AERODYNAMICS" ltp="3-0-0" crd="3" sub_type="ELECTIVE- II-VI"/>
boomsya commented 7 years ago

unable to parse? in my project all OK you just need write own class for your structure