org.w3c.dom.Document in JAVA

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

Upvotes: 0

Views: 292

Answers (1)

Robby Cornelissen
Robby Cornelissen

Reputation: 97120

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

Upvotes: 1

Related Questions