tibagni / SimpleXmlSerializer

A simple Android tool to help create XML from Java objects and also read XML Strings and convert into java objects.
2 stars 1 forks source link

How to parse Root Element Value? #2

Closed kachmul2004 closed 3 years ago

kachmul2004 commented 3 years ago

How how can I construct a class for this kind of XML?

< ?xml version="1.0" encoding="UTF-8"?>< response>OK< /response>

I am using Kotlin so my class is like this:

@XmlClass("response") class OKResponse { var value: String? = null }

However I get com.tiagobagni.simplexmlserializerlib.xml.XmlDeserializationException: Possibly malformed Xml"

Could you please help with a sample Java/Kotlin Class that can work with the above XML?

tibagni commented 3 years ago

Hi @kachmul2004 currently the code does not support

< ?xml version="1.0" encoding="UTF-8"?>
<response>
  OK
</response>

If you try and create a class like following

@XmlClass
public class SimpleResponseObject {
    @XmlField public String value;
}

It will generate/expect the following xml

< ?xml version="1.0" encoding="UTF-8"?>
<response>
  <value>
    OK
  </value>
</response>

However, you should not see any exception with this XML. The exception is because your XLM has spaces in the response tag and XmlPullParser is failing

I will take a look at the code to see if it is easy to modify to support your example (it's been a while since I haven't checked this code :P). But please note you should remove the extra spaces in your example XML

tibagni commented 3 years ago

There is a workaround if you only want to de-serialize the object: Create a response class with a single response @XmlField

@XmlClass
public class SimpleResponseObject {
    @XmlField public String response;
}

If you want to serialize, then the lib code will have to be modified to support it

kachmul2004 commented 3 years ago

@tibagni thanks for the response. I do not have blank spaces in my code, for some reason when I type the XML code on github, it removes the tags and only displays OK. That's why i added the space to bypass whatever was happening. In my actual code in the IDE, I do not have those spaces. Sorry about that

tibagni commented 3 years ago

Got it. No worries :) Could you try making a class like the example below and see if you can deserialize your xml? It worked for me

@XmlClass
public class SimpleResponseObject {
    @XmlField public String response;
}
tibagni commented 3 years ago

I created a new test case for this scenario: https://github.com/tibagni/SimpleXmlSerializer/blob/master/app/src/androidTest/java/com/tiagobagni/simplexmlserializer/DeserializerTests.java#L329-L341

It is passing. let me know if this works out for you or if you also need to serialize the class into that format

kachmul2004 commented 3 years ago

Got it. No worries :) Could you try making a class like the example below and see if you can deserialize your xml? It worked for me

@XmlClass
public class SimpleResponseObject {
    @XmlField public String response;
}

I had actually tried this before and it never worked. I have just tried it now and still doesn't work. I get the same response.

By the way, in Kotlin the code converts to:

@XmlClass class SimpleResponseObject { @XmlField var response: String? = null }

tibagni commented 3 years ago

Just tried with the following code

@XmlClass
class SimpleResponse {
    @XmlField
    var response: String? = null
    override fun toString(): String {
        return "SimpleResponse{" +
                "response='" + response + '\'' +
                '}';
    }
}

And it worked. Can you send me a sample project where this is failing?

kachmul2004 commented 3 years ago

@tibagni If you have some free time, are you able to check via TeamViewer? The program connects to a 4G router and the router returns the XML response

kachmul2004 commented 3 years ago

@tibagni By the way, I have a version of the program of the same program that runs in the console ,but I had issues installing the depency in IntelliJ. Does it work there?

kachmul2004 commented 3 years ago

@tibagni Hey, your code works great now :-) I believe my code should also work. The problem was that I needed to add the encoding when converting the response to string (thanks to this https://stackoverflow.com/a/48791187).

tibagni commented 3 years ago

Glad to hear it is working fine :)


@tibagni By the way, I have a version of the program of the same program that runs in the console ,but I had issues installing the depency in IntelliJ. Does it work there?

That is because it is an Android Lib. The only reason it is an Android Lib is because of XmlPullParser. The easiest way to use this in a plain java (or kitlin) project, like a console app is to copy my code inside your project as well as xmlpull parser code (https://android.googlesource.com/platform/libcore/+/9edf43dfcc35c761d97eb9156ac4254152ddbc55/xml/src/main/java/org/xmlpull/v1?autodive=0%2F) and compile all together

kachmul2004 commented 3 years ago

Thanks