Search code examples
javaxmlexceptionnullsax

Throwing SAXException


Learning xml parsing with Java, and am working in a test program to try things out as I go. All of the test System.out.println()'s were what I expected in the console except childElement was returning [name: null] and childElement.getNodeValue() was returning null rather than [name: "Branch Name"] and "Branch Name" respectively.

In an effort to correct that I started tweaking and of course broke the program further. Now, I'm throwing a SAXException (I've marked the line that I've narrowed it down to with a comment, although I'm not exactly clear on what a SAXException means, etc) when I try to run and I can't seem to get her back to where she was before.

Here's where the codes at now:

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

Any help with the SAXException is much appreciated, and after that I'll still probably need to figure out why the "name" element returns "null", but first things first :).

Updated Shown in comments what was causing the SAXException. Console is still displaying this however:

Test Results:

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

Finally

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

or

childElement.getTextContent()

Solution

  • the problem lies here

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

    you have just created an empty object of StringBuilder(xmlStringBuilder) and are expecting to parse that, so compiler throws a SAXParseException

    but you really need to do this

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

    as you appendage string contains the actual xml information

    or do this

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

    hope this helps!

    good luck!