문제

I get an error when using JDOM

org.jdom2.input.JDOMParseException: Error on line 4: cvc-elt.1: Cannot find the declaration of element 'rootElement'.

I have xsd menu.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema version="1.0"
           targetNamespace="http://www.w3schools.com/menu"
           elementFormDefault="qualified"
           attributeFormDefault="unqualified"
           xmlns="http://www.w3schools.com/menu"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="rootElement"  type="xs:string"/>
</xs:schema>

and xml menu.xml

<?xml version="1.0"?>
<rootElement xmlns="http://www.w3schools.com/menu"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.w3schools.com/menu menu.xsd">
</rootElement>

using

public class RunXml
{
    public static void main(String[] args) throws JDOMException, IOException
    {
        SAXBuilder builder = new SAXBuilder(XMLReaders.XSDVALIDATING);
        File    xml = new File("xml/menu.xml");
        InputStream inputStream = new FileInputStream(xml);
        Document document = builder.build(inputStream);
    }
}

Files are next. What's the problem?

도움이 되었습니까?

해결책

It could not find xsd. Problem solved so:

File xsdfile = new File("xml/menu.xsd");
XMLReaderJDOMFactory schemafac = new XMLReaderXSDFactory(xsdfile);
SAXBuilder builder = new SAXBuilder(schemafac);

다른 팁

Part of your problem is the 'obfuscation' you have related to the input source to JDOM's build() method. You are doing what appears to be a common bad practice of translating File values to InputStreams before submitting them to the build() method. In fact, you do not even need to translate the string value "xml/menu.xml" to a File object. Just do:

SAXBuilder builder = new SAXBuilder(XMLReaders.XSDVALIDATING);
Document document = builder.build("xml/menu.xml");

The String value "xml/menu.xml" is a System ID, and the SAX parser will interpret it, by default, as having a SystemID as something like file://./xml/menu.xml. Then, when it looks for the menu.xsd schema, it will look for it relative to that URI, and it will find file://./xml/menu.xsd

When you give the build method an InputStream instance, there is no System ID attached to the InputSource, and thus there is nowhere it can look to find the menu.xsd file.

You should only use the InputStream and Reader versions of the build(...) method when there is no other choice, and, if you do use those methods, you should try to use the versions where you can supply a System ID too.

I should update the documentation for those methods so that this issue is easier to avoid.

<rootElement xmlns="http://www.w3schools.com/menu"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.w3schools.com/menu menu.xsd">
</rootElement>

At this place in xml

<rootElement xmlns="http://www.w3schools.com/menu"

In xsd

targetNamespace="http://www.w3schools.com/menu"

Set a valid URI

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top