0

I'm working with XML files :

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<IDFS>
      <sunnydry>
            <idf>4.3562937</idf>
            <nbrOfRep>1.0</nbrOfRep>
      </sunnydry>
      <bresnahan>
            <idf>4.3562937</idf>
            <nbrOfRep>1.0</nbrOfRep>
      </bresnahan>
      <half>
            <idf>3.9534276</idf>
            <nbrOfRep>5.7123914</nbrOfRep>
      </half>
</IDFS>

and I use these functions to read any idf and nbrOfRep of a word

public float getIdfOfWordIndocument(String str)
    {
        try
        {
            return Float.parseFloat(document.getElementsByTagName(str.toLowerCase())
                    .item(0).getChildNodes().item(0).getTextContent()); 
        }
        catch(Exception e)
        {
            return 0.0f;
        }

    }

    // To read nbr of reputation of a word
    public float getNbrOfRepfWordIndocument(String str)
    {
        return Float.parseFloat(document.getElementsByTagName(str.toLowerCase())
                .item(0).getChildNodes().item(1).getTextContent());
    }

The first one give an error and the second one give the wrong result. However, when I change the code to this :

public float getIdfOfWordIndocument(String str)
        {
            try
            {
                return Float.parseFloat(document.getElementsByTagName(str.toLowerCase())
                        .item(0).getChildNodes().item(1).getTextContent()); 
            }
            catch(Exception e)
            {
                return 0.0f;
            }

        }

        // To read nbr of reputation of a word
        public float getNbrOfRepfWordIndocument(String str)
        {
            return Float.parseFloat(document.getElementsByTagName(str.toLowerCase())
                    .item(0).getChildNodes().item(3).getTextContent());
        }

Both functions work very well, but I could not understand why I have to make this change:

In 1st : .item(0) -> .item(1) and In 2nd : .item(1) -> .item(3)

I'm using this code to write the XML file:

    public void addToXML( String str, float idf, float nbrOfRep)
            {

                Element e = null;
                Element name = null;
                Element rootEle = (Element) document.getFirstChild();

                // create data elements and place them under root
                name = document.createElement(str.toLowerCase());
                rootEle.appendChild(name);

                e = document.createElement("idf");
                e.appendChild(document.createTextNode(Float.toString(idf)));
                name.appendChild(e);

                e = document.createElement("nbrOfRep");
                          e.appendChild(document.createTextNode(Float.toString(nbrOfRep)));
                name.appendChild(e);

                // doc.appendChild(rootEle);

                try{
                    Transformer tr =  TransformerFactory.newInstance().newTransformer();
                    tr.setOutputProperty(OutputKeys.INDENT, "yes");              
                     tr.setOutputProperty("{http://xml.apache.org/xslt}indent- amount","6");

                // send DOM to file             
try{
                    tr.transform(new DOMSource(document), new StreamResult(                         new FileOutputStream(filePath)));
            } 
catch (FileNotFoundException e)             
{
                    // TODO Auto-generated catch block
                    e.printStackTrace();            }

            } 
catch (TransformerException te)         
{           
System.out.println(te.getMessage());        
}
            }// end
Jongware
  • 22,200
  • 8
  • 54
  • 100
  • 1
    In addition to Robby's answer: you should find sub-elements by tag name instead of just getting child nodes by index. These text nodes might disappear if the XML is formatted differently. The code would be clearer and more robust. – JB Nizet Apr 07 '15 at 05:15

1 Answers1

1

There are text nodes between your elements:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<IDFS>
      <sunnydry><!-- Text node 0 here
         --><idf>4.3562937</idf><!-- Text node 2 here
         --><nbrOfRep>1.0</nbrOfRep>
      </sunnydry>
      <!-- ... -->
</IDFS>

So:

  1. Node 0: text node
  2. Node 1: idf element node
  3. Node 2: text node
  4. Node 3: nbrOfRep element node
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156