This question was first titled: How to remove -xmlnl tags when I receive JSON from a XML-HTTP-API server? but now I understand the concept of the problem.
This question is about a exception in Android regarding JSON translation. I do a HTTP GET request to a (not-mine) webserver made with c# and some api that, when I call it from a browser, the answer comes in XML and when I call it from my Android application the result is in JSON.
The thing is I am getting an JSON error like: Value {"-xmlnl:d4p1":"http://schemas..."} of Alternativas of type org.json.JSONObject cannot be converted to JSONArray
The code in XML has this king of content:
<Alternativas xmlns:d4p1="http://schemas.datacontract.org/2004/07/PainelMonitor.Model"/>
<Descricao>A primeira fraze que vocĂȘ ouviu hoje foi?</Descricao>
I am only interested in the <Descricao> content.
This {"-xmlnl:d4p1":"http://schemas..."} should not come to JSON and it maked my app crash.
So how could I take it off from my JSON? There are 'tens' of these invalid markups in the code, so I can't do it manually because it is a Array of JSON.
In your xsl you have to use **
exclude-result-prefixes=""
** attribute.
Accessing web service by Android application I usually use the Ksoap2 library and the code:
response.getProperty("field")
where "field" is the element name of XML, and response is the soapObject in which I have "captured" the WS answer.
The typical XML associate to this answer is:
<registry>
<cf>issjdeodk1292983ls</cf>
<name>John</name>
<surname>Doe</surname>
<sex>M</sex>
<message>a simple optional message</message>
</registry>
In this case when I try to access the field "message" there are no problems:
response.getProperty("message")
returns exactly the string "a simple optional message"
But my web service, if the message is not found (server side) return the following XML file
<registry>
<cf>issjdeodk1292983ls</cf>
<name>John</name>
<surname>Doe</surname>
<sex>M</sex>
</registry>
that is, the same previous XML without tag.
In this case if I use the code:
response.getProperty("message")
an error occurs.
Can I use a mechanism for getting the property only if exists?
I am not sure which library you use but when using xmlpullparser you can lookup if there is a tag like that by
if (parser.getName() == "message") { ....
I am new to Android development and I am wanting to parse a node only xml file using XML Resource Parser in android. I can only get it to parse an XML that contains attributes however I dont want my xml to contain attributes and I just want to pull out data contained in nodes only. Can I do this with XRP? There seem to only be methods for XML's containing attributes i.e. getAttributeValue for example
My xml looks like this:
<Streets>
<street>ABBEY WAY</street>
<district>WILLESBOROUGH</district>
<collectionday>FRIDAY</collectionday>
<jan>6,20</jan>
<feb>3,17</feb>
<mar>2,16,30</mar>
<apr>13,27</apr>
<may>11,25</may>
<jun>8,22</jun>
<jul>6,20</jul>
<aug>3,17,31</aug>
<sep>14,28</sep>
<oct>12,26</oct>
<nov>9,23</nov>
<dec>TBA</dec>
<street>ABBOTT WAY</street>
<district>TENTERDEN</district>
<collectionday>TUESDAY</collectionday>
<jan>10,24</jan>
<feb>7,21</feb>
<mar>6,20</mar>
<apr>3,17</apr>
<may>1,15,29</may>
<jun>12,26</jun>
<jul>10,24</jul>
<aug>7,21</aug>
<sep>4,18</sep>
<oct>2,16,30</oct>
<nov>13,27</nov>
<dec>TBA</dec>
<street>ACKERLEY COURT</street>
<district>STANHOPE</district>
<collectionday>THURSDAY</collectionday>
<jan>12,26</jan>
<feb>9,23</feb>
<mar>8,22</mar>
<apr>5,19</apr>
<may>3,17,31</may>
<jun>14,28</jun>
<jul>12,26</jul>
<aug>9,23</aug>
<sep>6,20</sep>
<oct>4,18</oct>
<nov>1,15,29</nov>
<dec>TBA</dec>
</Streets>
I want to create an array from the street node initially
Many thanks
You could also use Simple for Android. It's very easy and well documented:
http://simple.sourceforge.net/
IMHO a parser where I can get objects directly from a xml document is pretty neat :)
I am trying to convert data from database to XML in android. After that i am planning to post xml data to server using HttpPost?. But i am not sure how to convert string to xml in android.
I am looking for any sample code and methods.
Thanks in advance
Use the DOM from java library.
Refer:
http://developer.android.com/reference/javax/xml/parsers/DocumentBuilder.html#parse(java.lang.String)
http://exampledepot.com/egs/javax.xml.parsers/BasicDom.html
i am getting the text message and image from the .net web server by sending the xml parameters.
i need to display these messages and images using lazy list Concept.
I am getting messages by sending the xml parameters and display them in lazy list.
but in the above sample they using url to get the images but i am getting images from xml parameters.
i am getting confusion.can any one please help me how can i display images getting from xml parameters.
thanks in advance.
(let me add comment if any one doe's n't understand my question)
The SAX XML parser is available on Android and can read from any class that implements InputStream. Since URLConnection provides a way to return its underlying InputStream, it seems to me you could do something like the following...
URL xmlSource = new URL("some http path");
UrlConnection xmlConnection = xmlSource.openConnection();
org.xml.sax.InputSource xmlStream = new InputSource(xmlConnection.getInputStream());
I haven't tested this myself and Google is careful to warn that the included Sax implementation comes as-is, but it seems to me a good place to start. There are plenty of resources available online on using InputSource and implementing Sax callbacks.