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 8337167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:13:08+00:00 2026-06-09T04:13:08+00:00

I am making a practice application with the goal of reading data from an

  • 0

I am making a practice application with the goal of reading data from an RSS feed.

So far it has gone well, except my application encounters an issue with special characters. It reads the first special character within the node, and then moves to the next node.

Any help would be much appreciated, and sorry for the large code blocks that follow.

RSS Feed – http://www.usu.co.nz/usu-news/rss.xml

<title>Unitec hosts American film students</title>
<link>http://www.usu.co.nz/node/4640</link>
<description>&lt;p&gt;If you’ve been hearing American accents around the Mt Albert campus over the past week.</description>

Display Code

String xml = XMLFunctions.getXML();
Document doc = XMLFunctions.XMLfromString(xml);

NodeList nodes = doc.getElementsByTagName("item");

for (int i = 0; i < nodes.getLength(); i++) 
{                           
    Element e = (Element)nodes.item(i);
    Log.v("XMLTest", XMLFunctions.getValue(e, "title"));
    Log.v("XMLTest", XMLFunctions.getValue(e, "link"));
    Log.v("XMLTest", XMLFunctions.getValue(e, "description"));  
    Log.v("XMLTest", XMLFunctions.getValue(e, "pubDate"));
    Log.v("XMLTest", XMLFunctions.getValue(e, "dc:creator"));
}

Reader Code

public class XMLFunctions 
{

public final static Document XMLfromString(String xml)
{

    Document doc = null;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

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

    } catch (ParserConfigurationException e) {
        System.out.println("XML parse error: " + e.getMessage());
        return null;
    } catch (SAXException e) {
        System.out.println("Wrong XML file structure: " + e.getMessage());
        return null;
    } catch (IOException e) {
        System.out.println("I/O exeption: " + e.getMessage());
        return null;
    }

    return doc;

}

/** Returns element value
  * @param elem element (it is XML tag)
  * @return Element value otherwise empty String
  */
 public final static String getElementValue( Node elem ) {
     Node kid;
     if(elem != null)
     {
         if (elem.hasChildNodes())
         {
             for(kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling())
             {
                 if( kid.getNodeType() == Node.TEXT_NODE  )
                 {
                     return kid.getNodeValue();
                 }
             }
         }
     }
     return "";
 }

 public static String getXML(){  
        String line = null;

        try {

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://www.usu.co.nz/usu-news/rss.xml");

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            line = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
        } catch (MalformedURLException e) {
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
        } catch (IOException e) {
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
        }

        return line;

}

public static int numResults(Document doc){     
    Node results = doc.getDocumentElement();
    int res = -1;

    try{
        res = Integer.valueOf(results.getAttributes().getNamedItem("count").getNodeValue());
    }catch(Exception e ){
        res = -1;
    }

    return res;
}

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

Output

Unitec hosts American film students
http://www.usu.co.nz/node/4640
<
Wed, 01 Aug 2012 05:43:22 +0000
Phillipa
  • 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-09T04:13:12+00:00Added an answer on June 9, 2026 at 4:13 am

    Your function

    public final static String getElementValue( Node elem ) {
        Node kid;
        if(elem != null)
        {
            if (elem.hasChildNodes())
            {
                for(kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling())
                {
                    if( kid.getNodeType() == Node.TEXT_NODE  )
                    {
                        return kid.getNodeValue();
                    }
                }
            }
        }
        return "";
    }
    

    is returning the first text node under the given element. A chunk of text within a single tag can be split into multiple text nodes, and this tends to happen in the presence of special characters.

    You should probably append all the text nodes into a string for the return value.

    Something approximately like this might work:

    public final static String getElementValue( Node elem ) {
        if ((elem == null) || (!(elem.hasChildNodes())))
            return "";
    
        Node kid;
        StringBuilder builder = new StringBuilder();
        for(kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling())
        {
            if( kid.getNodeType() == Node.TEXT_NODE  )
            {
                builder.append(kid.getNodeValue());
            }
        }
        return builder.toString();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm thinking about making a simple web application to practice custom tags, EL, ...
I'm making a simple PHP application with Twitter/Facebook integration to practice OOP concepts and
Is it possible and is there such a practice? I am making an application
Is there a best practice for making an entity immutable? Users create exercises in
Is there a best practice for making private methods in classes static? I have
What's the best practice of making a variable that would be accessible by almost
I'm making a little alarm clock as a project to practice as I'm just
I got a question about good practice. I'm making a simple game and, I'm
Is it bad practice to hack the main application_top.php file of osCommerce for making
I have an application that loads lots of data into memory (this is because

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.