Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8068735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:47:05+00:00 2026-06-05T12:47:05+00:00

Okay I’m really sorry if this question has been asked, but I can’t seem

  • 0

Okay I’m really sorry if this question has been asked, but I can’t seem to find any good help on this.

I coded myself a little API for my android app using PHP and Codeigniter. It works through GET requests, so I’ll send it some info and it’ll return something. When I send it a login request, I want it to validate the user data, and if correct return the user’s credentials (uid, email, etc). All of this information can either be returned as formatted XML or a JSON array. My question is which would be easier to work with in Java, and can you help me with a quick example?

Any help is much appreciated.

EDIT:

Requested Code

                // Parse JSON array
                JSONObject obj = (JSONObject) JSONValue.parse(response);
                String uid = (String) obj.get("uid");
                String email = (String) obj.get("email");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:47:07+00:00Added an answer on June 5, 2026 at 12:47 pm

    From my experience, JSON is much easier to work with.

    This is some data formatted in XML:

    <book>
        <person>
          <first>Kiran</first>
          <last>Pai</last>
          <age>22</age>
        </person>
        <person>
          <first>Bill</first>
          <last>Gates</last>
          <age>46</age>
        </person>
        <person>
          <first>Steve</first>
          <last>Jobs</last>
          <age>40</age>
        </person>
    </book>
    

    Here is sample code to read the XML file:

    import java.io.File;
    import org.w3c.dom.Document;
    import org.w3c.dom.*;
    
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.DocumentBuilder;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException; 
    
    public class ReadAndPrintXMLFile{
    
        public static void main (String argv []){
        try {
    
                DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                Document doc = docBuilder.parse (new File("book.xml"));
    
                // normalize text representation
                doc.getDocumentElement ().normalize ();
                System.out.println ("Root element of the doc is " + 
                     doc.getDocumentElement().getNodeName());
    
    
                NodeList listOfPersons = doc.getElementsByTagName("person");
                int totalPersons = listOfPersons.getLength();
                System.out.println("Total no of people : " + totalPersons);
    
                for(int s=0; s<listOfPersons.getLength() ; s++){
    
    
                    Node firstPersonNode = listOfPersons.item(s);
                    if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){
    
    
                        Element firstPersonElement = (Element)firstPersonNode;
    
                        //-------
                        NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
                        Element firstNameElement = (Element)firstNameList.item(0);
    
                        NodeList textFNList = firstNameElement.getChildNodes();
                        System.out.println("First Name : " + 
                               ((Node)textFNList.item(0)).getNodeValue().trim());
    
                        //-------
                        NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
                        Element lastNameElement = (Element)lastNameList.item(0);
    
                        NodeList textLNList = lastNameElement.getChildNodes();
                        System.out.println("Last Name : " + 
                               ((Node)textLNList.item(0)).getNodeValue().trim());
    
                        //----
                        NodeList ageList = firstPersonElement.getElementsByTagName("age");
                        Element ageElement = (Element)ageList.item(0);
    
                        NodeList textAgeList = ageElement.getChildNodes();
                        System.out.println("Age : " + 
                               ((Node)textAgeList.item(0)).getNodeValue().trim());
    
                        //------
    
    
                    }//end of if clause
    
    
                }//end of for loop with s var
    
    
            }catch (SAXParseException err) {
            System.out.println ("** Parsing error" + ", line " 
                 + err.getLineNumber () + ", uri " + err.getSystemId ());
            System.out.println(" " + err.getMessage ());
    
            }catch (SAXException e) {
            Exception x = e.getException ();
            ((x == null) ? e : x).printStackTrace ();
    
            }catch (Throwable t) {
            t.printStackTrace ();
            }
            //System.exit (0);
    
        }//end of main
    }
    

    And this is the same data formatted in JSON:

    {"book":
        [ 
          {"person":
              {"last":"Pai","age":22,"first":"Kiran"}
          },
          {"person":
              {"last":"Gates","age":46,"first":"Bill"}
          },
          {"person":
              {"last":"Jobs","age":40,"first":"Steve"}
          }
        ]
    }
    

    And the sample code to read the JSON:

    JSONObject obj = (JSONObject) JSONValue.parse(jsonString);
    JSONArray arr = (JSONArray) obj.get("book");
    for (int i = 0; i < arr.size(); i++) {
        JSONObject person = (JSONObject) arr.get(i);
        String first = (String) person.get("first");
        String last = (String) person.get("last");
        String age = (String) person.get("age");
        System.out.println("Person " + i +" : first = "+ first + ", last = " + last + ", age = " + age);
    }
    

    From the 2 piece of sample codes, you can see that JSON is much easier to read, write, and format.

    XML is more commonly used as the industry standard for exchanging information, while JSON is commonly used to exchange information via REST-based APIs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

okay so this is probably a soft pitch question for sombody, but I want
Okay people, I'm sure someone has had this issue and can help me out.
OKay, this is probably pretty noob, but I couldn't find how to solve it.
Okay, first some background, I can't use any javascript library except YUI for this
Okay, this is a pretty weird question, but the situation is this. I've got
Okay, this is probably a very basic question; but, I'm just getting back in
Okay, This may be really simple or it may not even be possible, or
Okay, I have done a bit of searching online and found this thread, but
Okay, none of the previous questions I have seen with this error seem to
Okay, so this is a long long shot but here it goes... When IE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.