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

  • Home
  • SEARCH
  • 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 8972675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:16:20+00:00 2026-06-15T18:16:20+00:00

I am writing an application in Java for Android (SDK v8) that parses XML

  • 0

I am writing an application in Java for Android (SDK v8) that parses XML and puts the entries into a ListView. This part works fine. I am parsing the XML with a DocumentBuilder, which is terminating the strings it’s outputting after an entity – excluding the entity itself. The entities I am using are standard entites &(quot, amp, apos, lt, gt); I have also tried using numeric entities in my source XML (e.g. &# 38; without the space, just so you can see what I’m outputting) and this leads to a crash of my app, with logcat reporting “unterminated entity ref”.

To test that I am not using invalid XML, I have tried viewing the XML with Google Chrome – which displays it perfectly. The entry blah & blah.txt is truncated to blah. The XML I am parsing is below:

EDIT: Much shorter XML sample

<?xml version="1.1"?>  
<root>  
<object>  
<id>ROOT</id>  
<type>directory</type>  
<name>../</name>  
</object>  
<object>  
<id>09F010C143B84573A36C50F3EF7E0708</id>  
<type>file</type>  
<name>blah &amp; blah.txt</name>  
</object>   
<object>  
<id>85CF028B838D4E0096C081B987C97045</id>  
<type>file</type>  
<name>Epilist.m3u</name>  
</object>  
</root>  

EDIT: XML parsing class
EDIT2: Below is a complete class that (with the help of others) should now be bug free. Anyone is welcome to use this class – I am providing it as Public Domain code. You do not need to reference that I originally produced this code to use it. It is designed for Android, but by replacing references to ‘Log.e’ it can easily be used on any Java platform as far as I know.

package tk.dtechsoftware.mpclient;

import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.util.Log;

public class XMLParser {
    public String getXmlFromUrl(String url) {
        String xml = null;

        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);

            // HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // return XML
        return xml;
    }

    public Document getDomElement(String xml) {
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setExpandEntityReferences(false);
        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is);

        } catch (ParserConfigurationException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (SAXException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (IOException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        }
        // return DOM
        return doc;
    }

    public String getValue(Element item, String str) {
        NodeList n = item.getElementsByTagName(str);
        return n.item(0).getTextContent(); 
    }

}
  • 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-15T18:16:22+00:00Added an answer on June 15, 2026 at 6:16 pm

    I don’t think its guaranteed that an element node has only one child node containing its text content. The contents can as well be split across multiple child nodes.

    Your getElementValue method can probably be replace by a simple call to elem.getTextContent().

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

Sidebar

Related Questions

I am writing an application that will allow an android phone and java application
I am writing a java application that takes schema-bounded XML as input and needs
So I'm writing a Java application that uses Simple to store data as xml
I'm writing an application (on android) that uses both java code, and native code.
I am writing a function in Java for an Android application that uses a
I am writing an application in Java that places an icon in the system
I am writing a Java application that writes a number of files, and later
Using mysql 5.5.2 on windows (Wamp) I'm writing a Java application that use several
I am writing an application that SDK 10+. I would like all of my
I'm writing an application that will ship in two versions: Android and PC version.

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.