P3ntium
P3ntium

Reputation: 11

The markup in the document preceding the root element must be well-formed in .dtd file

I have next files:

<!ELEMENT notes (note+)>
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

and

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE notes SYSTEM "validation.dtd">
<notes>
    <note>
        <to>Alice</to>
        <from>Bob</from>
        <heading>Reminder</heading>
        <body>Don't forget our meeting!</body>
    </note>
    <note>
        <to>John</to>
        <from>Jane</from>
        <heading>Invalid Tag</heading>
        <body>This should be </body>
    </note>
    <note>
        <to>Tom</to>
        <from>Sara</from>
        <heading>Empty Tag</heading>
        <body>hello</body>
    </note>
</notes>

and my java validator


import org.xml.sax.Attributes;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import java.io.File;

public class SAXParserTest {
    public static void main(String[] args) {
        parseWithDTDValidation("C:\\Users\\ilapa\\Desktop\\university\\4_course_1_sem\\Проектування Веб сервісів\\lab_5\\lab5\\src\\main\\resources\\validation.dtd");
//        parseWithXSDValidation("C:\\Users\\ilapa\\Desktop\\university\\4_course_1_sem\\Проектування Веб сервісів\\lab_5\\lab5\\src\\main\\resources\\index.xml",
//                "C:\\Users\\ilapa\\Desktop\\university\\4_course_1_sem\\Проектування Веб сервісів\\lab_5\\lab5\\src\\main\\resources\\XSDvalidation.xsd");
    }

    private static void parseWithDTDValidation(String filePath) {
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setValidating(true); // Увімкнути валідацію по DTD
            SAXParser parser = factory.newSAXParser();
            parser.parse(new File(filePath), new CustomHandler());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void parseWithXSDValidation(String filePath, String schemaPath) {
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
            Schema schema = schemaFactory.newSchema(new File(schemaPath));
            factory.setSchema(schema);
            SAXParser parser = factory.newSAXParser();
            parser.parse(new File(filePath), new CustomHandler());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class CustomHandler extends DefaultHandler {
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) {
        System.out.println("Start Element: " + qName);
    }

    @Override
    public void endElement(String uri, String localName, String qName) {
        System.out.println("End Element: " + qName);
    }

    @Override
    public void characters(char[] ch, int start, int length) {
        System.out.println("Characters: " + new String(ch, start, length).trim());
    }

    @Override
    public void error(SAXParseException e) {
        System.out.println("Error: " + e.getMessage());
    }

    @Override
    public void fatalError(SAXParseException e) {
        System.out.println("Fatal Error: " + e.getMessage());
    }

    @Override
    public void warning(SAXParseException e) {
        System.out.println("Warning: " + e.getMessage());
    }
}

Compiler get me next error: `org.xml.sax.SAXParseException; systemId: file:src/main/resources/validation.dtd; lineNumber: 1; columnNumber: 3; The markup in the document preceding the root element must be well-formed.

I checked every symbol in my .dtd validation and xml file, but could not find any syntax error,

Upvotes: 1

Views: 29

Answers (1)

BenjyTec
BenjyTec

Reputation: 10887

It looks like you are trying to parse the validation.dtd file in this code line:

parseWithDTDValidation("C:\\Users\\ilapa\\Desktop\\university\\4_course_1_sem\\Проектування Веб сервісів\\lab_5\\lab5\\src\\main\\resources\\validation.dtd");

You instead should pass the path to the xml file itself.

parseWithDTDValidation("C:\\Users\\ilapa\\Desktop\\university\\4_course_1_sem\\Проектування Веб сервісів\\lab_5\\lab5\\src\\main\\resources\\index.xml");

As your xml already links to the DTD file, the SAX Parser will automatically use that for validation.

<!DOCTYPE notes SYSTEM "validation.dtd">

This also is outlined in the documentation of setValidating:

Specifies that the parser produced by this code will validate documents as they are parsed. By default the value of this is set to false.
Note that "the validation" here means a validating parser as defined in the XML recommendation. In other words, it essentially just controls the DTD validation. (except the legacy two properties defined in JAXP 1.2.)

Upvotes: 0

Related Questions