学习使用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()

最佳答案

问题出在这里

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);


希望这可以帮助!

祝好运!

关于java - 抛出SAXException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30205209/

10-11 22:30