[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1455
  • Last Modified:

Android XML Parsing error PImust not start with XML

Hi

Im new to Android and this is my first attempt at building a listview  and consuming a REST webservice into the ListView. I have built a REST webservice using visual Studio 2010, tested and working fine. The XML result is the following:

- <ArrayOfBookDetails xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
- <BookDetails>
  <BookId>2</BookIdId>
  <BookName>Book1</BookName>
  <Level>Keen Reader</Level>
  </BookDetails>
  </ArrayOfBookDetails>

This is done using the folowing Contract and Datamember:

[OperationContract]
        [WebGet(RequestFormat = WebMessageFormat.Xml,
            ResponseFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Bare
            )]
        BookDetails[] GetAllBooks();

[DataContract(Name = "BookDetails", Namespace = "")]
    public class bookDetails
    {
        [DataMember]
        public int BookId { get; set; }
        [DataMember]
        public string BookName { get; set; }
        [DataMember]
        public string Level{ get; set; }
      }

All good so far...

Next I published my webservce into IIS7 and again tested and is running. Now the fun part, i have built an ap that is supposed to call the url, parse it then add to the list view. However as soon as the activity loads i get the folowing error:

PI must not start with xml (position:unknown xm@1:5 in java.io.StringReader@44f31a30

This is how im calling the webservice in android:

static final String URL = "http://IP here:4657/Service.svc/GetAllBooks";
      // XML node keys
      static final String KEY_BOOKDETAILS = "BookDetails"; // parent node
      static final String KEY_BOOKID = "Bookid";
      static final String KEY_BOOKNAME = "BookName";
      static final String KEY_LEVEL= "Level";


One good thing i suppose is that the app is hitting the webservce. So is the problem with the format of the XML?
0
wilko100
Asked:
wilko100
  • 3
1 Solution
 
PCableGuyCommented:
XML tags are case sensitive and I noticed that static final String KEY_BOOKID = "Bookid" has a small case "i" but the XML tag has a capital "I" in BookId.

Try using:
static final String KEY_BOOKID = "BookId";
0
 
PCableGuyCommented:
You also might need to start your XML file with a declaration, that PI error seems to be complaining that your file starts with an XML tag. PI stands for processing instruction.

This is an example of an XML declaration:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
0
 
wilko100Author Commented:
Ah yeah, ok i have amended the BookId but i still get the error. So it must be the declaration but how do i change that and is that set in the DataContract or Operationcontract code section?
0
 
PCableGuyCommented:
I'm not too familair with DataContract or Operationcontract. If you point me to a web resource, I can read about it and get back with you.

In the meantime, it might work if you simply open the XML file in Notepad and merely add the line, save the file, close Notepad, and run the code again. This will also help determine if that's the solution to prevent the error.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now