简体   繁体   English

JAVA中的org.w3c.dom.Document

[英]org.w3c.dom.Document in JAVA

I'm working with XML files : 我正在使用XML文件:

<?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 我使用这些功能读取单词的任何idf和nbrOfRep

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) 在第1个:.item(0)-> .item(1)和在第2个:.item(1)-> .item(3)

I'm using this code to write the XML file: 我正在使用以下代码来编写XML文件:

    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

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 节点0:文本节点
  2. Node 1: idf element node 节点1: idf元素节点
  3. Node 2: text node 节点2:文本节点
  4. Node 3: nbrOfRep element node 节点3: nbrOfRep元素节点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM