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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:39:46+00:00 2026-06-16T02:39:46+00:00

Looking for someone to look over my simple code. I am rather new to

  • 0

Looking for someone to look over my simple code. I am rather new to what I am doing and know that I am probably just making a simple mistake somewhere.

I am parsing an xml file that is over http and trying to print out the text associated with the elements to the screen, and make an object that is populated by the text in those elements.

I can get all of the elements and associated text printed but the objects all have a null value in their fields. Let me know if I anything needs to be explained better.

Code is found below:

Object Class:

   package com.entities;



public class StageOfLife
{

private String endDate;
private String offerData;
private String offerType;
private String redemption;
private String startDate;
private String termsConditions;
private String title;
private String merchantDescription;
private String merchantLogo;
private String merchantName;

public StageOfLife() {

}

public StageOfLife(String endDate, String offerData, String offerType,
        String redemption, String startDate, String termsConditions,
        String title, String merchantDescription, String merchantLogo,
        String merchantName)
{

// Getters Setters HEre

 public String toString() {

 StringBuffer buffer = new StringBuffer();

 buffer.append(endDate);
 buffer.append("\n");
 buffer.append(offerData);
 buffer.append("\n");
 buffer.append(offerType);
 buffer.append("\n");
 buffer.append(redemption);
 buffer.append("\n");
 buffer.append(startDate);
 buffer.append("\n");
 buffer.append(termsConditions);
 buffer.append("\n");
 buffer.append(title);
 buffer.append("\n");
 buffer.append(merchantDescription);
 buffer.append("\n");
 buffer.append(merchantLogo);
 buffer.append("\n");
 buffer.append(merchantName);

 return buffer.toString();
 }

}

And here is is the class with the methods and main:

package com.xmlparse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.entities.StageOfLife;



public class XmlParserStage
{
 Document dom;
 DocumentBuilder db;
 List<StageOfLife> myStageList;

 public XmlParserStage(){
        //create a list to hold the StageOfLife objects
        myStageList = new ArrayList<StageOfLife>();
    }


private void parseXmlFile(){
    //get the factory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    try {

        //Using factory get an instance of document builder
        db = dbf.newDocumentBuilder();

        //parse to get DOM representation of the XML file
    dom = db.parse("http:/url/goes/here");


    } catch(ParserConfigurationException pce) {
        pce.printStackTrace();
    } catch(SAXException se) {
        se.printStackTrace();
    } catch(IOException ioe) {
        ioe.printStackTrace();
    }
}

 private void parseDocument() {


    //get the root element
    Element docEle = dom.getDocumentElement();

    //get a nodelist of elements
    NodeList nl = docEle.getElementsByTagName("Offer");
    if(nl != null && nl.getLength() > 0) {
        for(int i = 0 ; i < nl.getLength(); i++) {

            //get the elements 
            Element solEl = (Element)nl.item(i);

            //get the StageOfLife object
            StageOfLife sol = getStageOfLife(solEl);

            //add it to list
          myStageList.add(sol);
        }
    }
}

/*
  take an <offer> element and read the values in, create
  an StageOfLife object and return it
*/
private StageOfLife getStageOfLife(Element solEl) {

/*
  for each <offer> element get the values
*/

    String endDate = getTextValue(solEl,"EndDate");
    String offerData = getTextValue(solEl,"OfferData");
    String offerType = getTextValue(solEl,"OfferType");
    String redemption = getTextValue(solEl,"Redemption");
    String startDate = getTextValue(solEl,"StartDate");
    String termsConditions = getTextValue(solEl,"TermsConditions");
    String title = getTextValue(solEl,"Title");
    String merchantDescription = getTextValue(solEl,"MerchantDescription");
    String merchantLogo = getTextValue(solEl,"MerchantLogo");
    String merchantName = getTextValue(solEl,"MerchantName");

    //Create a new StageOfLife object with the value read from the xml nodes
    StageOfLife sol = new StageOfLife(endDate, offerData, offerType,
             redemption, startDate, termsConditions,
             title, merchantDescription, merchantLogo,
             merchantName);

    return sol;
}

/*
  take a xml element and the tag name, look for the tag and get
  the text content
 */

private String getTextValue(Element ele, String tagName) {

    String textVal = null;
    NodeList nl = ele.getElementsByTagName(tagName);
    if(nl != null && nl.getLength() > 0) {
        Element el = (Element)nl.item(0);
        textVal = el.getFirstChild().getNodeValue();

        System.out.print(el + ":" + textVal);
        System.out.println();

    }

    return textVal;
}


/*
 Calls getTextValue and returns a int value
*/
private int getIntValue(Element ele, String tagName) {

    return Integer.parseInt(getTextValue(ele,tagName));
}

private void printData(){

    System.out.println("Number of Offers: '" + myStageList.size() + "'.");

    Iterator it = myStageList.iterator();
    while(it.hasNext()) {
        System.out.println(it.next().toString());

    }
}

public void run() {

    //parse the xml file and get the dom object
    parseXmlFile();

    //get each stageoflife element and create a StageOfLife object
    parseDocument();

    //Iterate through the list and print the data
    printData();
}

public static void main(String [] args) throws ClientProtocolException, IOException      {

  XmlParserStage xmlParser = new XmlParserStage();

    xmlParser.httpClient();

    xmlParser.run(); 

}

}
  • 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-16T02:39:48+00:00Added an answer on June 16, 2026 at 2:39 am

    your constructor is not doing anything!

    public StageOfLife(String endDate, String offerData, String offerType,
            String redemption, String startDate, String termsConditions,
            String title, String merchantDescription, String merchantLogo,
            String merchantName)
    {
      // set the data
      this.endDate = endDate;
      ...
    }
    

    But much better is to use Java XML Binding jaxb. it automatically maps java classes to xml

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

Sidebar

Related Questions

Can someone recommend a simple c# code generator. I just looking something with methods
I've been looking over the following VB code: http://www.codeproject.com/KB/vb/toggleNetworkConn.aspx If you look under The
I was looking through someone's code one day and I was annoyed how they
I am looking for ways that someone could maliciously pause a Flash movie -
I'm looking to have it so that if someone enters information in a form,
I've noticed yesterday by looking into my apache error log that someone tried to
If I'm looking at someone else's rails app, what's the first place to look
Someone has asked me to look at their code to do a few little
Im looking for a simple (if I can call it that) mapping tool for
I'm looking to extract pitches from a sound signal. Someone on IRC just explained

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.