首页 > 解决方案 > document builder add html tags to doc

问题描述

This is actually related to Hibernate when it tries to load the persistence.xml locally.

Here is the stacktrace and error
ERROR javax.persistence.PersistenceException: Invalid persistence.xml.
Error parsing XML (line-1 : column -1): cvc-elt.1.a: Cannot find the declaration of element 'HTML'.

    at org.hibernate.ejb.packaging.PersistenceXmlLoader.loadURL(PersistenceXmlLoader.java:145)
    at org.hibernate.ejb.packaging.PersistenceXmlLoader.deploy(PersistenceXmlLoader.java:169)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:317)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)

Actually the persistence xml is plain (I minimized it from the larger one as it makes no difference)

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
</persistence>

The issue is this line in the documentbuilder

    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            docBuilder.setEntityResolver(resolver);
    doc = docBuilder.parse(source);

>The doc after parsed has HTML tags in it which it should not because its just a xml. I traveresed the nodes in the doc and got below elements

Found element HTML
Found element HEAD
Found element BODY
Found element PERSISTENCE

This is why XSD schema validation fails as it was not expecting html elements.

Anyone knows why and how to get over this.

标签: hibernatexml-parsing

解决方案


发现问题。我在类路径中有 nekohtml-1.9.22.jar。它是一个解析器,也是一个标签平衡器。它被用作默认解析器,因为它没有在 xml 中找到 HTML 标签,所以添加了它。奇怪的是它不应该是 XML 的默认值。无论如何从类路径中删除它解决了这个问题。


推荐阅读