需要帮助才能阅读文本文件

时间:2013-03-23 13:05:08

标签: java xml file-io

我是JAVA的新手,我想阅读文本文件并用XML编写,这是我的输入:

  1. R.-J。 Roe,J。Appl.Phys。 36,2024(1965)。
  2. 和输出但是:

            <ref id="1">
            <label>1</label>
            <citation-alternatives>
                <mixed-citation>R.-J. Roe, J. Appl.Phys. 36, 2024 (1965).</mixed-citation>
            </citation-alternatives>
        </ref>
    

    在许多情况下,这个输入有两行,它们之间没有空格,如下所示:

    1. R.-J。鱼子,

      学家Appl.Phys。 36,2024(1965)。

    2. 输出将是:

              <ref id="1">
              <label>1</label>
              <citation-alternatives>
                  <mixed-citation>R.-J. Roe, </mixed-citation>
              </citation-alternatives>
          </ref>
      
          <ref id="1">
              <label>1</label>
              <citation-alternatives>
                  <mixed-citation>J. Appl.Phys. 36, 2024 (1965).</mixed-citation>
              </citation-alternatives>
          </ref>
      

      现在我的问题是我怎么能把这两行看成是第一个输出? 这是我的代码:

      try {
                  String strLine;
                  String num="";
                  String mix="";
                  DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
                  DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
      
                  // Back element
                  Document doc = docBuilder.newDocument();
                  Element rootElement = doc.createElement("Back");
                  doc.appendChild(rootElement);
      
                  // ref-list element
                  Element reflist = doc.createElement("ref-list");
                  rootElement.appendChild(reflist);
      
                  while( (strLine = br.readLine()) != null)                   
      
                  {                       
                      if (strLine.equals("")) {
                          continue;
                      }
                      int dotIndex = strLine.indexOf(".");
      
                      num = strLine.substring(0,dotIndex);
                      mix = strLine.substring(dotIndex+2,strLine.length());
      
      
      
                      // ref element
                      Element ref= doc.createElement("ref");
                      reflist.appendChild(ref);
      
                      // set attribute of ref element
                      Attr attr = doc.createAttribute("id");
                      attr.setValue(num);
                      ref.setAttributeNode(attr);
      
                      // label element
                      Element label = doc.createElement("label");
                      ref.appendChild(label);
                      label.setTextContent(num);
      
                      // citation-alternatives element
                      Element citationalternatives = doc.createElement("citation-alternatives");
                      ref.appendChild(citationalternatives);
      
                      // mixed-citation element
                      Element mixedcitation = doc.createElement("mixed-citation");
                      citationalternatives.appendChild(mixedcitation);
                      mixedcitation.setTextContent(mix);
                  }
      

2 个答案:

答案 0 :(得分:1)

在将strLine插入元素之前,检查是否strLine.endsWith(“,”),如果是,则读取下一行(依此类推)并附加到第一个strLine。

答案 1 :(得分:0)

您的代码在读取包含额外换行符的记录时创建两个<ref>记录的原因是您使用换行符来定义记录何时开始。

您需要清楚地定义标记开始记录的内容。

例如,也许所有记录都以一个数字后跟一个句点开头。也许它更具可预测性:它们都以序号开头,后跟一段时间。利用这个逻辑,我们可以创建一个有条件的新元素:

    Element ref= doc.createElement("ref");
    while( (strLine = br.readLine()) != null) {                       
        if (strLine.equals(""))
            continue;
        int dotIndex = strLine.indexOf(".");
        num = strLine.substring(0,dotIndex);
        mix = strLine.substring(dotIndex+2,strLine.length());
        if(refStart(strLine)) {
            ref= doc.createElement("ref");
            reflist.appendChild(ref);
        }
        //now decide how to parse the input - maybe it will be different depending on 
        //whether the line we just read starts a new record or continues one from
        //the previous line.
    }


    public boolean refStart(String line) {
        if(line.length()<2) 
            return false;
        int dotIndex = strLine.indexOf(".");
        if(dotIndex<=0 || dotIndex>5) //assuming largest value is 99999
            return false;
        String numString = strLine.substring(0,dotIndex);
        for(int i=0; i<numString.length(); i++) {
            if(!Character.isDigit(numString.charAt(i) )
               return false;
        }
        return true;
    }
相关问题