Question

I am currently working on a program that takes the value of the 'data type' selected from a combo box (via .getSelectedIndex()) and then drills down toe the 'first record' and returns it as a String array.

The combo box is populated by the same XML file and I am coding in Java using the DOM parser.

XML code snippet:

    <?xml version="1.0" encoding="UTF-8"?>
   <dealershipParsers>
<Parser id="ADPParser2">
    <data type="Sales" />
        <first record = "DEAL-NO"/>        
    <data type="Service" />
        <first record ="RO"/>                
    <data type="Email" />
        <first record ="CONTROL"/>        
</Parser>

The Java code I'm currently using to attempt to get and return the 'first record':

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = factory.newDocumentBuilder();
    Document document = docBuilder.parse(fileName);

    //Searches all text 
    document.getDocumentElement().normalize();

    //Gets elements from xml 

    Element elementArray = document.getDocumentElement();
    NodeList listResources = elementArray.getElementsByTagName("Parser");

    Element elemChild = (Element) listResources.item(item);      
    NodeList dataChildren =  elemChild.getElementsByTagName("data"); 

    Element elemFirst = (Element) dataChildren.item(item);
    NodeList firstChildren = elemFirst.getElementsByTagName("first");

    int tam = firstChildren.getLength();
    String[] vecResources = new String[tam];

    for (int i = 0; i < tam; i++) 
    {
       Element elem = (Element) firstChildren.item(0);      
       vecResources[i] =  elem.getAttribute("record"); // change to type
    }        

    //returns an array with all the types of data
    return vecResources;

I have managed to pinpoint my problem being at the transition somewhere between 'dataChildren', 'elemFirst' and 'firstChildren'. 'dataChildren' has a count in it but when I do a debug in netbeans it shows that 'tam' is equal to 0 as is 'firstChildren'.

Why can't it pass the value from one nodelist to the other? What am I missing?

Any advice would be appreciated.

Was it helpful?

Solution

Your XML structure does not match with what you're code "expects". Your code seems to expect a hierarchical XML input, where each Parser holds data nodes and each data holds first nodes:

<Parser id="ADPParser2">
    <data type="Sales">
        <first record="DEAL-NO" />
    </data>
    <data type="Service">
        <first record="RO" />
    </data>
    <data type="Email">
        <first record="CONTROL" />
    </data>
</Parser>

However, your XML input is flat: the data and first nodes are direct children of a Parser. Your indentation seems to suggest otherwise (which probably caused this confusion), but it really is equivalent to:

<Parser id="ADPParser2">
    <data type="Sales" />
    <first record="DEAL-NO" />
    <data type="Service" />
    <first record="RO" />
    <data type="Email" />
    <first record="CONTROL" />
</Parser>

I can see two solutions:

  • Fix the structure of your XML input to be hierarchical, like your code expects it.
  • Fix your code to work with the "flat" style (although I don't think that's what you want).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top