【问题标题】:Throwing SAXException抛出 SAXException
【发布时间】:2015-05-13 03:36:11
【问题描述】:

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

为了纠正这一点,我开始调整,当然进一步破坏了程序。现在,当我尝试运行并且我可以似乎无法让她回到原来的位置。

这里是现在的代码:

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”,但首先要做的事情是 :)。

更新 在 cmets 中显示了导致 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()

【问题讨论】:

  • ByteArrayInputStream input = ... 之前移动xmlStringBuilder.append(appendage); 之后(感谢@Vihar)我不再收到SAXException,但在我的控制台中仍然得到“null”而不是“Branch Name”。

标签: java xml exception null sax


【解决方案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); 

希望这会有所帮助!

祝你好运!

【讨论】:

  • 非常感谢!我之前移动了这些行,现在我将xmlStringBuilder.append(appendage); 放在ByteArrayInputStream input -= ... 之前,这样就处理了SAXException。仍然为 childElement 返回“null”...也许我需要一个 toString 调用?
  • @JamesLingo 尝试做childElement.getTextValue()
  • @JamesLingo 如果答案对您有帮助,请接受答案
  • _@Vihar 肯定是在计划它,只是睡着了 :) 出于某种原因 Eclipse 不接受childElement.getTextValue(),并推荐childElement.getNodeValue(),它一直没有工作。也许我搞砸了继承/需要实现功能/或导入库?
  • 试过childElement.getNodeValue().toString() 就成功了!我想我应该已经意识到 NodeValue 需要被格式化......我还是宁愿跳过 NodeValue() getter 并使用 .getTextValue() 或类似的东西。关于我遗漏的任何想法?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
  • 1970-01-01
  • 2013-03-15
  • 2011-07-26
  • 2018-01-04
  • 1970-01-01
相关资源
最近更新 更多