Results 1 to 2 of 2
  1. #1
    mindscape777 is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default XML Node.getNodeValue Problem

    Hi,

    I am new to the community but I figured if there is anywhere I can get a reasonable explanation for my problem it would likely be here.

    I have the following class that I created to identify certain countries that are found in an XML node as a value:

    public static int isCountry(String countryName)
    {

    int countryCtr;
    int countryInstance = 0;
    String country ="";
    String[] countryFilters = new String[8];

    countryFilters[0]="US";
    countryFilters[1]="CA";
    countryFilters[2]="MEXICO";
    countryFilters[3]="CHILE";
    countryFilters[4]="ARGENTINA";
    countryFilters[5]="UNIVERSE";
    countryFilters[6]="LATIN AMERICA";
    countryFilters[7]="NORTH AMERICA";

    for(countryCtr=0;countryCtr<countryFilters.length; ++countryCtr)
    {
    country = countryFilters[countryCtr].toString();
    if(country == countryName)
    {
    System.out.println("MATCH FOUND!!!");
    ++countryInstance;
    }
    }
    if(countryInstance > 0)
    {
    return countryInstance;
    }
    return countryInstance;
    }

    The problem I am encountering is that if I test this class from main() and pass a string value that is in the array it works just as intended. :)

    But if I implement this class in my XML parsing code and dynamically pass in the value using the Node.getNodeValue() method it doesn't work.

    I have even added the toString() method and explicitly assigned the value to a string and it doesn't work. :confused:

    Anyone having a possible clue as to why this isn't working that could provide some insight on the issue, I would greatly appreciate the help.

    Thx.

  2. #2
    mindscape777 is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default

    For anyone interested, I managed to find the answer to the problem on my own via the post of another person experiencing the same issue. Their response is as follows:

    > You need to use .equals("and") to compare the strings. the "=="
    > operator tests for reference equality. You have two String objects in
    > memory, which have different references, so the "==" test fails.
    > However, if you tell them to compare themselves, they'll figure out
    > they mean the same thing.

    So the following alteration of my code resolved the issue:

    if(country.equals(countryName))
    {
    System.out.println("MATCH FOUND!!!");
    ++countryInstance;
    }

    :)

Similar Threads

  1. [SOLVED] Node traversal using TreeViewer and SelectionEvent
    By xcallmejudasx in forum Advanced Java
    Replies: 1
    Last Post: 11-07-2008, 09:33 PM
  2. java DOM: append text to node
    By newbieal in forum New To Java
    Replies: 2
    Last Post: 10-10-2008, 07:18 PM
  3. How to disabled a node.
    By smartsubroto in forum New To Java
    Replies: 32
    Last Post: 07-01-2008, 07:30 AM
  4. Node selection in jtree
    By Preethi in forum AWT / Swing
    Replies: 4
    Last Post: 06-19-2008, 11:25 PM
  5. How to Transmit data from one node to another
    By swimberl in forum Networking
    Replies: 2
    Last Post: 01-04-2008, 08:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •