抛出SAXException

时间:2015-05-13 03:36:11

标签: java xml exception null sax

学习使用Java进行xml解析,并且我正在测试程序中尝试解决问题。所有测试System.out.println()都是我在控制台中所期望的,除了childElement返回[name:null]并且childElement.getNodeValue()返回null而不是[name:“Branch Name”]和“分支名称“分别为。

为了纠正我开始调整,当然还打破了程序。现在,我正在抛出一个SAXException(我已经标记了我将其缩小到一条评论的行,虽然我并不完全清楚SAXException意味着什么等)当我尝试运行时我可以似乎让她回到原来的位置。

以下是现在的代码:

package uploaders;

import javax.swing.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import java.awt.EventQueue;
import java.io.*;

public class Test {
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { 
        JFileChooser chooser = new JFileChooser();
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        StringBuilder xmlStringBuilder = new StringBuilder();
        String appendage = "<?xml version=\"1.0\"?><branch0><name>Branch Name</name></branch0>";
        ByteArrayInputStream input = new ByteArrayInputStream(xmlStringBuilder.toString().getBytes("UTF-8"));

        chooser.setCurrentDirectory(new File("."));
        chooser.setMultiSelectionEnabled(false);
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

        int result = chooser.showOpenDialog(null);

        if (result == JFileChooser.APPROVE_OPTION) { 
            System.out.println("Test Results:");
            System.out.println();
            System.out.println(chooser.getSelectedFile().getPath()); 

            Document doc = builder.parse(input); //originally org.w3c.dom.Document...this line is where SAXException is thrown.
            Element root = doc.getDocumentElement(); 
            NodeList children = root.getChildNodes();

            System.out.println(root.getTagName());
            xmlStringBuilder.append(appendage); //forgot to move this up too (thanks @Vihar)

            for (int i = 0; i < children.getLength(); i++) {
                Node child = children.item(i);
                if (child instanceof Element) { 
                    Element childElement = (Element) child; 
                    System.out.println(childElement.getTagName());
                    System.out.println(childElement.getNodeValue());
                    System.out.println(childElement); }
            }
        }   
    }
}

非常感谢SAXException的任何帮助,之后我仍然可能需要弄清楚为什么“name”元素返回“null”,但首先要做的事情是:)。

更新 在注释中显示导致SAXException的原因。然而,控制台仍在显示:

Test Results:

/Users/.../Documents/java/Test/./bin
branch0
name
null           //I'm expecting "Branch Name"
[name: null]   //and "[name: Branch Name]"

最后

childElement.getNodeValue() //needed + .toString() to work or:
childElement.getNodeValue().toString()

childElement.getTextContent()

1 个答案:

答案 0 :(得分:1)

问题出在这里

ByteArrayInputStream input = new ByteArrayInputStream(xmlStringBuilder.toString().getBytes("UTF-8"));

你刚刚创建了一个StringBuilder(xmlStringBuilder)的空对象,并期望解析它,因此编译器会抛出SAXParseException

但你确实需要这样做

ByteArrayInputStream input = new ByteArrayInputStream(appendage.toString().getBytes("UTF-8"));

因为appendage字符串包含实际的xml信息

或执行此操作

xmlStringBuilder.append(appendage);   //I have just changed the sequence of these lines in your code
ByteArrayInputStream input = new ByteArrayInputStream(xmlStringBuilder.toString().getBytes("UTF-8"));
Document doc = builder.parse(input); 

希望这有帮助!

祝你好运!