简体   繁体   English

为什么我得到 SAXparseException “必须声明元素类型”,即使它是在将 XML 文件加载到属性 Object 时声明的?

[英]why am I getting SAXparseException “Element type must be declared” eventhough it was declared while loading XML file to Properties Object?

I am a newbie to java and eclipse.我是 java 和 eclipse 的新手。 I was trying to load an XML file using properties.loadFromXML() and tried to read properties's names.我试图使用 properties.loadFromXML() 加载 XML 文件并尝试读取属性的名称。 but I ended with getting SAXparseException saying "Element type must be declared", even though I have defined DTD for my XML file.但我最终得到 SAXparseException 说“必须声明元素类型”,即使我已经为我的 XML 文件定义了 DTD。 Could someone help me?有人可以帮助我吗? this is my XML file这是我的 XML 文件

<?xml version="1.0"?>
<!DOCTYPE test [
<!ELEMENT test (name, price, hsn)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT hsn (#PCDATA)>
]>
<test>
    <name>groundnut</name>
    <price>5.00</price>
    <hsn>ABCDE</hsn>
</test>

this is my java code: import java.util.Properties;这是我的 java 代码:导入 java.util.Properties; import java.util.Set;导入 java.util.Set;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import java.io.File;
import java.io.IOException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

import java.io.*;


public class test 
{
    public static void main(String args[]) throws Exception
    {
        Properties p = new Properties();
        InputStream is = new FileInputStream("resources/test.XML");
        p.loadFromXML(is);
        System.out.print(p.stringPropertyNames());

    }
}

this is the output I got:这是我得到的 output:

Exception in thread "main" java.util.InvalidPropertiesFormatException: jdk.internal.org.xml.sax.SAXParseException; Element type "test" must be declared.
at java.base/jdk.internal.util.xml.PropertiesDefaultHandler.load(PropertiesDefaultHandler.java:85)
at java.base/java.util.Properties.loadFromXML(Properties.java:956)
at test.main(test.java:28)

Caused by: jdk.internal.org.xml.sax.SAXParseException;引起:jdk.internal.org.xml.sax.SAXParseException; Element type "test" must be declared.必须声明元素类型“test”。 at java.base/jdk.internal.util.xml.PropertiesDefaultHandler.startElement(PropertiesDefaultHandler.java:169) at java.base/jdk.internal.util.xml.impl.ParserSAX.parse(ParserSAX.java:470) at java.base/jdk.internal.util.xml.impl.ParserSAX.parse(ParserSAX.java:411) at java.base/jdk.internal.util.xml.impl.ParserSAX.parse(ParserSAX.java:374) at java.base/jdk.internal.util.xml.impl.SAXParserImpl.parse(SAXParserImpl.java:97) at java.base/jdk.internal.util.xml.PropertiesDefaultHandler.load( at java.base/jdk.internal.util.xml.PropertiesDefaultHandler.startElement(PropertiesDefaultHandler.java:169) at java.base/jdk.internal.util.xml.impl.ParserSAX.parse(ParserSAX.java:470) at java .base/jdk.internal.util.xml.impl.ParserSAX.parse(ParserSAX.java:411) at java.base/jdk.internal.util.xml.impl.ParserSAX.parse(ParserSAX.java:374) at java .base/jdk.internal.util.xml.impl.SAXParserImpl.parse(SAXParserImpl.java:97) at java.base/jdk.internal.util.xml.PropertiesDefaultHandler.load( PropertiesDefaultHandler.java:83)... 2 more属性DefaultHandler.java:83)... 2 更多

Importing XML using Properties.loadFromXML() does not support the use of custom DTDs.使用Properties.loadFromXML()导入 XML 不支持使用自定义 DTD。 It only supports the DTD documented in the Properties Javadoc .它仅支持Properties Javadoc 中记录的 DTD

It's not clear why you think you can use an alternative DTD to read in properties like this.目前尚不清楚为什么您认为可以使用替代 DTD 来读取这样的属性。

You will instead have to modify your XML document to match the Properties DTD:您将不得不修改您的 XML 文档以匹配属性 DTD:

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <entry key="name">groundnut</entry>
    <entry key="price">5.00</entry>
    <entry key="hsn">ABCDE</entry>
</properties>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “ org.testng.TestNGException:org.xml.sax.SAXParseException; 在侦听器上工作时,必须声明元素类型“侦听器” - “org.testng.TestNGException: org.xml.sax.SAXParseException; Element type ”Listeners" must be declared. while working on listeners org.xml.sax.SAXParseException; lineNumber:17; columnNumber:61; 必须为元素类型“属性”声明属性“值” - org.xml.sax.SAXParseException; lineNumber: 17; columnNumber: 61; Attribute “value” must be declared for element type “property” 休眠错误:org.xml.sax.SAXParseException:必须声明元素类型“休眠映射” - Hibernate Error: org.xml.sax.SAXParseException: Element type “hibernate-mapping” must be declared org.xml.sax.SAXParseException; lineNumber:10; columnNumber:24; 必须为元素类型“ bean”声明属性“ singleton” - org.xml.sax.SAXParseException; lineNumber: 10; columnNumber: 24; Attribute “singleton” must be declared for element type “bean” 为什么我的程序“必须被捕获或被宣布被抛出”? - Why am I getting "must be caught or declared to be thrown" on my program? 必须声明元素类型“struts”。 at (null:26:9) org.xml.sax.SAXParseException 在 Struts 2 中使用 JQuery 网格插件 - Element type "struts" must be declared. at (null:26:9) org.xml.sax.SAXParseException in Struts 2 using JQuery Grid plugin Spring 4.3.4 with weblogic 12.1 导致 org.xml.sax.SAXParseException;必须为元素类型“bean”声明属性“singleton” - Spring 4.3.4 with weblogic 12.1 Causing org.xml.sax.SAXParseException;Attribute “singleton” must be declared for element type “bean” 如何修复“错误创建文档实例。 原因:org.xml.sax.SAXParseException;必须为元素类型“映射器”声明属性“ class”。 - How to fix 'Error creating document instance. Cause: org.xml.sax.SAXParseException;Attribute “class” must be declared for element type “mapper”.' 为什么我得到一个变量未声明的错误? - Why am I getting a varriable not declared error? 必须为元素类型“property”声明获取“属性”ref“。” - Getting “Attribute ”ref“ must be declared for element type ”property“.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM