调用时XML解析器中的空指针异常

时间:2016-02-21 16:59:10

标签: java xml

我在java中创建一个XML解析器,但是当我尝试根据字符串参数创建文件时,它说:

Exception in thread "main" java.lang.NullPointerException
    at java.io.File.<init>(File.java:277)
    at parsers.XML.XMLParser.main(XMLParser.java:21)

这是我的XML解析器的代码:

package parsers.XML;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.File;
import java.io.IOException;
import org.xml.sax.SAXException;
import java.util.*;
public class XMLParser {
    public static String XMLfile;
    public static String mainTag;
    public static String root;
    public static List<XMLElementCompiler> elements = new ArrayList<>();
    /**
    * @param xmlfile
    */
    public XMLParser(String xmlfile) {
        XMLfile = xmlfile;
    }
    public static void main(String[] args) {
        try {
            File file = new File(XMLfile);
            DocumentBuilder DB = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document DOC = DB.parse(file);
            root = DOC.getDocumentElement().getNodeName();
            if(DOC.hasChildNodes()) {
                logNodes(DOC.getChildNodes());
            }
        }catch(ParserConfigurationException | SAXException | IOException e) {
            System.out.println(e.getMessage());
        }
        XMLParser test = new XMLParser("/Users/Ian/school.xml");
    }
    private static void logNodes(NodeList nodes) {
        for(int i = 0;i < nodes.getLength();i ++) {
            Node node = nodes.item(i);
            String name = null, textValue = null;
            List<String> attrs = new ArrayList<>(), attrVals = new ArrayList<>();
            if(node.getNodeType() == Node.ELEMENT_NODE) {
                name = node.getNodeName();
                textValue = node.getTextContent();
                if(node.hasAttributes()) {
                    NamedNodeMap attrMap = node.getAttributes();
                    for(int j = 0;j < attrMap.getLength();j ++) {
                        Node attr = attrMap.item(j);
                        if(attr.getNodeName().equals("id") && attr.getNodeValue().equals("main")) {
                            mainTag = node.getNodeName();
                        }
                        attrs.add(attr.getNodeName());
                        attrVals.add(attr.getNodeValue());
                    }
                }
            }
            elements.add(new XMLElementCompiler(name, textValue, attrs, attrVals));
        }
    }
    /**
     * @return 
     */
    public List<XMLElementCompiler> getCompiledData() {
        return elements;
    }
}

只有在主要内容中我才能将文件编入系统:File file = new File("/path/to/xmlfile.xml");。这个文件的重点是能够解析给定的任何命名XML文件,有没有办法做到这一点?我测试时,我所拥有的XML文件格式也正确,你看到的XMLElementCompiler是这样的:

package parsers.XML;
import java.util.*;
public class XMLElementCompiler {
    public static String elementName;
    public static String elementTextValue;
    public static List<String> attrNames = new ArrayList<>();
    public static List<String> attrValues = new ArrayList<>();
    /**
     * @param name
     * @param textValue
     * @param attrs
     * @param attrVals
     */
    public XMLElementCompiler(String name, String textValue, List attrs, List attrVals) {
        elementName = name;
        elementTextValue = textValue;
        attrNames = attrs;
        attrValues = attrVals;
    }
    /**
     * @param method
     * @return 
     */
    public String getEle(String method) {
        String ret;
        switch (method) {
            case "elementName":
                ret = elementName;
            break;
            case "elementTextValue":
                ret = elementTextValue;
            break;
            default:
                ret = null;
            break;
        }
        return ret;
    }
    /**
     * @param method
     * @return 
     */
    public List allAttr(String method) {
        List ret;
        switch (method) {
            case "names":
                ret = attrNames;
            break;
            case "values":
                ret = attrValues;
            break;
            default:
                ret = null;
            break;
        }
        return ret;
    }
    /**
     * @param method
     * @param i
     * @return 
     */
    public String getAttr(String method, int i) {
        String ret;
        switch (method) {
            case "names":
                ret = attrNames.get(i);
            break;
            case "values":
                ret = attrValues.get(i);
            break;
            default:
                ret = null;
            break;
        }
        return ret;
    }
}

我的测试XML文件是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<school>
    <principle>Mrs. Moen</principle>
    <grade gradeLevel="7th">
        <pier>Mathew</pier>
        <pier>Sebass</pier>
        <pier>Lauren</pier>
    </grade>
    <grade gradeLevel="8th">
        <pier>Hannah</pier>
        <pier>Isaac</pier>
        <pier>Ricky</pier>
    </grade>
    <teachers>
        <teacher class="Math and Science">Mr. Bond</teacher>
        <teacher class="American History">Ms. Smith</teacher>
        <teacher class="7th Grade L.A.">Madame Parkinson</teacher>
    </teachers>
</school>

任何帮助将不胜感激,谢谢。

0 个答案:

没有答案