1

I am trying to parse an XML file I made and pull information out of it. I am basing off of this tutorial. The parsing is connecting to the XML file ok and evaluates the first tag but will crash afterwards as seen in the Output. Attached below are the XML File layout, Java parsing code, and the Console Output.

Any idea why it is not reading the next tag of the XML, and if so, what should I change?


XML File:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<filepath>/mnt/sdcard/Audio_Recorder/anonymous22242.3gp</filepath>
<filename>anonymous22242.3gp</filename>
<annotation>
    <file>anonymous22242.3gp</file>
    <timestamp>0:06</timestamp>
    <note>test1</note>
</annotation>
<annotation>
    <file>anonymous22242.3gp</file>
    <timestamp>0:09</timestamp>
    <note>lol</note>
</annotation>
<annotation>
    <file>anonymous22242.3gp</file>
    <timestamp>0:09</timestamp>
    <note>lolol</note>
</annotation>

Java File:

private static String fileDirectory;
private final static ArrayList<String> allFileNames = new ArrayList<String>();
private final static ArrayList<String[]> allAnnotations = new ArrayList<String[]>();
private static String[] currentAnnotation = new String[3];

public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {

        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser playbackParser = factory.newSAXParser();

        DefaultHandler handler = new DefaultHandler() {

            boolean audioFullPath = false;
            boolean audioName = false;
            boolean annotationFile = false;
            boolean annotationTimestamp = false;
            boolean annotationNote = false;

            public void startElement(String uri, String localName,
                    String qName, Attributes attributes)
                    throws SAXException {

                System.out.println("Start Element :" + qName);

                if (qName.equalsIgnoreCase("filepath")) {
                    audioFullPath = true;
                }

                if (qName.equalsIgnoreCase("filename")) {
                    audioName = true;
                }

                if (qName.equalsIgnoreCase("file")) {
                    annotationFile = true;
                }

                if (qName.equalsIgnoreCase("timestamp")) {
                    annotationTimestamp = true;
                }

                if (qName.equalsIgnoreCase("note")) {
                    annotationNote = true;
                }

            }

            public void endElement(String uri, String localName,
                    String qName) throws SAXException {

                System.out.println("End Element :" + qName);

            }

            public void characters(char ch[], int start, int length)
                    throws SAXException {

                if (audioFullPath) {
                    String filePath = new String(ch, start, length);
                    System.out.println("Full Path : " + filePath);
                    fileDirectory = filePath;
                    audioFullPath = false;
                }

                if (audioName) {
                    String fileName = new String(ch, start, length);
                    System.out.println("File Name : " + fileName);
                    allFileNames.add(fileName);
                    audioName = false;
                }

                if (annotationFile) {
                    String fileName = new String(ch, start, length);
                    currentAnnotation[0] = fileName;
                    annotationFile = false;
                }

                if (annotationTimestamp) {
                    String timestamp = new String(ch, start, length);
                    currentAnnotation[1] = timestamp;
                    annotationTimestamp = false;
                }
                if (annotationNote) {
                    String note = new String(ch, start, length);
                    currentAnnotation[2] = note;
                    annotationNote = false;
                    allAnnotations.add(currentAnnotation);
                }

            }

        };

        File file = new File(
                "c:\\Documents and Settings\\anonymous.xml");
        InputStream inputStream = new FileInputStream(file);
        Reader xmlReader = new InputStreamReader(inputStream, "UTF-8");

        InputSource xmlSource = new InputSource(xmlReader);
        xmlSource.setEncoding("UTF-8");

        playbackParser.parse(xmlSource, handler);

        System.out.println(fileDirectory);
        System.out.println(allFileNames);
        System.out.println(allAnnotations);

    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

Output:

Start Element :filepath
Full Path : /mnt/sdcard/Audio_Recorder/anonymous22242.3gp
End Element :filepath
org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$TrailingMiscDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at javax.xml.parsers.SAXParser.parse(Unknown Source)
    at main.main(main.java:131)

2 Answers 2

5

That's not XML. There must be a single root element containing all of the rest.

Sign up to request clarification or add additional context in comments.

1 Comment

Yup. I feel dumb. Thanks though!
2

Quite clearly, your XML isn't well formed. It must have a base (root) element. I have added a <config> element.

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<config>
    <filepath>/mnt/sdcard/Audio_Recorder/anonymous22242.3gp</filepath>
    <filename>anonymous22242.3gp</filename>
    <annotation>
        <file>anonymous22242.3gp</file>
        <timestamp>0:06</timestamp>
        <note>test1</note>
    </annotation>
    <annotation>
        <file>anonymous22242.3gp</file>
        <timestamp>0:09</timestamp>
        <note>lol</note>
    </annotation>
    <annotation>
        <file>anonymous22242.3gp</file>
        <timestamp>0:09</timestamp>
        <note>lolol</note>
    </annotation>
</config>

1 Comment

Yeah that fixed it... Whoooooooooooops.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.