线程" main"中的例外情况java.lang.Error:未解析的编译

时间:2014-11-25 21:41:06

标签: java xml-parsing compiler-errors apache-poi

所以我知道有关此错误的类似问题。我知道这是因为代码有问题,这就是为什么它没有编译。我的问题是,我不知道我的代码中的错误在哪里,我无法弄明白。只是为了总结程序,它是一个XML解析器,它创建一个TreeNode数据结构并将其导出到excel中。在我添加它导出到excel的功能之前,它工作得非常好。但是当我使用apache poi将它导出为ex​​cel时出于某种原因存在编译问题,但是我看不出这可能是导致错误的原因。任何人都可以发现这个问题发生在代码中的什么/哪里?干杯

编辑: - 道歉!这是错误: - 线程“main”中的异常java.lang.Error:未解决的编译问题: 在HMDB.XML.main(XML.java:200)

这很奇怪,因为它似乎出现的编码行是一个空白行。它似乎发生在subElements方法中,直接位于这段代码之下: -

    '} else if (event == XMLStreamConstants.CHARACTERS
            && !reader.isWhiteSpace()) {

        newNode.getElement().setValue(reader.getText())'

这是代码: -

包HMDB;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;

import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
public class XML {

private int rowNum = 0;
private int columnNum = 0;

public XMLStreamReader xmlInput() {

    XMLStreamReader reader = null;

    try {

        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        InputStream file = new FileInputStream(
                "Users/kevlar/Dropbox/PhD/Java/Metabolites/src/HMDB/HMDB00316.xml");
        reader = inputFactory.createXMLStreamReader(file);

    } catch (XMLStreamException e) {
        System.err.println("XMLStreamException : " + e.getMessage());

    } catch (FactoryConfigurationError e) {
        System.err.println("FactoryConfigurationError : " + e.getMessage());

    } catch (FileNotFoundException e) {
        System.err.println("FileNotFoundException : " + e.getMessage());

    }
    return reader;
}

private void findElements(String input) throws XMLStreamException {

    TreeNode[] children;
    Workbook wb = new HSSFWorkbook();
    Sheet sheet1 = wb.createSheet("Metabolites");
    String[] elementsSplit = input.split("\\s*,\\s*");
    xmlInput();
    XMLStreamReader reader = xmlInput();
    reader.nextTag();

    do {
        if (reader.getEventType() == XMLStreamConstants.START_ELEMENT
                && reader.getLocalName() == "metabolite") {
            children = mainElements(reader).children();

            printValues(children, elementsSplit, sheet1);
            children = null;
        } else {
            reader.next();
        }
    } while (reader.hasNext());

    reader.close();

    try {
        FileOutputStream output = new FileOutputStream("HMDB.xls");
        wb.write(output);
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

private void printValues(TreeNode[] children, String[] elementsSplit,
        Sheet sheet1) {

    rowNum++;
    Row row = sheet1.createRow(rowNum);
    Cell cell = row.createCell(columnNum);

    for (int i = 0; i < children.length; i++) {
        TreeNode element = children[i];
        String elementName = element.getElementName();

        for (int j = 0; j < elementsSplit.length; j++) {
            String searchName = elementsSplit[j];

            if (searchName.equals(elementName)) {

                if (element.hasChildren()) {
                    recurse(element.children(), cell);
                } else {
                    columnNum++;
                    cell.setCellValue(element.getElementValue());
                    System.out.println("Element:- "
                            + element.getElementName() + " | Value:- "
                            + element.getElementValue());
                }
            }
        }
    }

    cell = null;
}

private void recurse(TreeNode[] children, Cell cell) {

    for (int i = 0; i < children.length; i++) {
        TreeNode node = children[i];
        if (node.hasChildren()) {
            System.out.println("Element:- " + node.getElementName()
                    + " and it's subelements:- ");
            recurse(node.children(), cell);
        }

        else if (!node.hasChildren()) {
            columnNum++;
            cell.setCellValue(node.getElementValue());
            System.out.println("Element:- " + node.getElementName()
                    + " | Value:- " + node.getElementValue());
        }
    }
}

private TreeNode mainElements(XMLStreamReader reader)
        throws XMLStreamException {

    Element rootElement = new Element();
    rootElement.setName(reader.getLocalName());
    TreeNode root = new TreeNode(rootElement);

    int level = 1;

    do {
        int event = reader.next();

        if (event == XMLStreamConstants.START_ELEMENT) {

            Element element = new Element();
            element.setName(reader.getLocalName());
            TreeNode node = new TreeNode(element);
            level++;

            if (level == 2) {
                root.add(subElements(reader, node));
                level--;

            }

        } else if (event == XMLStreamConstants.END_ELEMENT) {
            level--;
        }

    } while (level > 0);

    return root;
}

private TreeNode subElements(XMLStreamReader reader, TreeNode node)
        throws XMLStreamException {

    int level = 1;

    TreeNode newNode = new TreeNode();
    newNode = node;

    do {
        int event = reader.next();

        if (event == XMLStreamConstants.START_ELEMENT) {

            Element subElement = new Element();
            subElement.setName(reader.getLocalName());
            TreeNode subNode = new TreeNode(subElement);
            level++;

            if (level == 2) {
                newNode.add(subElements(reader, subNode));
                level--;
            }

        } else if (event == XMLStreamConstants.CHARACTERS
                && !reader.isWhiteSpace()) {

            newNode.getElement().setValue(reader.getText());

        } else if (event == XMLStreamConstants.END_ELEMENT) {
            level--;
        }

    } while (level > 0);

    return newNode;
}

public static void main(String[] args) throws XMLStreamException {

    XML test = new XML();
    test.findElements("accession, inchikey");

}

}

package HMDB;

public class Element {

private String name;
private String value;

public void setName(String name){
    this.name = name;
}

public void setValue(String value){
    this.value = value;
}

public String getName(){
    return name;
}

public String getValue(){
    return value;
}

}


package HMDB;

public class TreeNode{


/**
 * This is the nodes parent node. If it is the root of the tree, 
 * then it will be null
 */
private TreeNode parent;

/**
 * An array of all this nodes children nodes. If it's a leaf node 
 * i.e. node with no children, then length will be zero
 */
private TreeNode[] children = new TreeNode[0];

private Element element;

public TreeNode()
{

}

/**
 * Assigns the element object of this tree node to the one passed
 * through
 * @param element
 */
public TreeNode(Element element){
    this.element = element;
}

public void setElement(Element element){
    this.element = element;
}

public Element getElement(){
    return element;
}

public String getElementName(){
    return element.getName();
}

public String getElementValue(){
    return element.getValue();
}



 /**
   * Adds the <code>child</code> node to this container making this its parent.
   * 
   * @param child is the node to add to the tree as a child of <code>this</code>
   * 
   * @param index is the position within the children list to add the
   *  child.  It must be between 0 (the first child) and the
   *  total number of current children (the last child).  If it is
   *  negative the child will become the last child.
   */
  public void add (TreeNode child, int index)
  {
    // Add the child to the list of children.
    if ( index < 0 || index == children.length )  // then append
    {
      TreeNode[] newChildren = new TreeNode[ children.length + 1 ];
      System.arraycopy( children, 0, newChildren, 0, children.length );
      newChildren[children.length] = child;
      children = newChildren;
    }
    else if ( index > children.length )
    {
      throw new IllegalArgumentException("Cannot add child to index " + index + ".  There are only " + children.length + " children.");
    }
    else  // insert
    {
      TreeNode[] newChildren = new TreeNode[ children.length + 1 ];
      if ( index > 0 )
      {
        System.arraycopy( children, 0, newChildren, 0, index );
      }
      newChildren[index] = child;
      System.arraycopy( children, index, newChildren, index + 1, children.length - index );
      children = newChildren;
    }

    // Set the parent of the child.
    child.parent = this;
  }

  /**
   * Adds the <code>child</code> node to this container making this its parent.
   * The child is appended to the list of children as the last child.
   */
  public void add (TreeNode child)
  {
    add( child, -1 );
  }

  /**
   * Gets a list of all the child nodes of this node.
   * 
   * @return An array of all the child nodes.  The array will
   *  be the size of the number of children.  A leaf node
   *  will return an empty array, not <code>null</code>.
   */
  public TreeNode[] children ()
  {
    return children;
  }

  /**
   * Returns if this node has children or if it is a leaf
   * node.
   * 
   * @return <code>true</code> if this node has children; <code>false</code>
   *  if it does not have any children.
   */
  public boolean hasChildren ()
  {
    if ( children.length == 0 )
    {
      return false;
    }
    else
    {
      return true;
    }
  }

  /**
   * Gets the position of this node in the list of siblings
   * managed by the parent node.  This node can be obtained
   * by <code>this = parent.children[this.index()]</code>.
   * 
   * @return The index of the child array of this node's
   *  parent.  If this is the root node it will return -1.
   */
  public int index ()
  {
    if ( parent != null )
    {
      for ( int i = 0; ; i++ )
      {
        Object node = parent.children[i];

        if ( this == node )
        {
          return i;
        }
      }
    }

    // Only ever make it here if this is the root node.
    return -1;
  }

}

1 个答案:

答案 0 :(得分:1)

这些不是所有的进口商品。您缺少TreeNode和Element对象的导入。您需要导入这些类才能在文件中使用它们。这些是基于您发布的代码的唯一编译错误。

相关问题