【问题标题】:DTD validation from JaxB classesJaxB 类的 DTD 验证
【发布时间】:2019-03-12 17:39:54
【问题描述】:

我必须从 DTD 文件开始创建一些用于 XML 生成的 java 类。

我使用了 jxc 实用程序:

xjc -dtd -d generatedsrc -p com.examples log4j.dtd

我可以生成 xml,但它被接收方拒绝,除了这个 DTD 之外我没有其他信息。如何根据 XML 验证 DTD? 例如,在 DTD 中可以定义一个我在编组之前没有在我的对象中设置的必填字段,我怎样才能找到我缺少的内容?

【问题讨论】:

    标签: xml jaxb marshalling dtd


    【解决方案1】:

    有一些针对 xml 验证 DTD 的示例。为方便起见,我将在此处粘贴一个,但最后的链接。

    对于包含 dtd 的给定 xml,invalid_dtd.xml:

    <?xml version="1.0"?>
    <!-- invalid_dtd.xml
     - Copyright (c) 2002-2014 HerongYang.com, All Rights Reserved.
    -->
    <!DOCTYPE dictionary [
     <!ELEMENT dictionary (note, word+)>
     <!ELEMENT note ANY>
     <!ELEMENT word (update?, name, definition+, usage*)>
     <!ELEMENT update EMPTY>
     <!ATTLIST update 
      date CDATA #REQUIRED 
      editor CDATA #IMPLIED
     >
     <!ELEMENT name (#PCDATA)>
     <!ATTLIST name is_acronym (true | false) "false">
     <!ELEMENT definition (#PCDATA)>
     <!ELEMENT usage (#PCDATA | i)*>
     <!ELEMENT i (#PCDATA)>
     <!ENTITY herong "Dr. Herong Yang">
    ]>
    <dictionary>
     <note>Copyright (c) 2014 by &herong;</note>
     <word>
      <name is_acronym="true" language="EN">POP</name>
      <definition>Post Office Protocol</definition>
      <definition>Point Of Purchase</definition>
     </word>
     <word>
      <update date="2014-12-23"/> 
      <name is_acronym="yes">XML</name>
      <definition>eXtensible Markup Language</definition>
      <note>XML comes from SGML</note>
     </word>
     <word>
      <update editor="Herong Yang"/> 
      <name>markup</name>
      <definition>The amount added to the cost price to calculate 
    the selling price - <i>Webster</i></definition>
     </word>
    </dictionary>
    

    运行一些检查的简单类:

    public class DOMValidator {
        public static void main(String[] args) {
            try {
                File x = new File("invalid_dtd.xml");
                DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                documentBuilderFactory.setValidating(true);
                DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    
                ErrorHandler h = new MyErrorHandler();
                documentBuilder.setErrorHandler(h);
                documentBuilder.parse(x);
            } catch (ParserConfigurationException e) {
                System.out.println(e.toString());
            } catch (SAXException e) {
                System.out.println(e.toString());
            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }
        private static class MyErrorHandler implements ErrorHandler {
            public void warning(SAXParseException e) throws SAXException {
                System.out.println("Warning: ");
                printInfo(e);
            }
            public void error(SAXParseException e) throws SAXException {
                System.out.println("Error: ");
                printInfo(e);
            }
            public void fatalError(SAXParseException e)
                    throws SAXException {
                System.out.println("Fattal error: ");
                printInfo(e);
            }
            private void printInfo(SAXParseException e) {
                System.out.println("   Public ID: "+e.getPublicId());
                System.out.println("   System ID: "+e.getSystemId());
                System.out.println("   Line number: "+e.getLineNumber());
                System.out.println("   Column number: "+e.getColumnNumber());
                System.out.println("   Message: "+e.getMessage());
            }
        }
    }
    

    你可以使用它吗?

    原文来源:http://www.herongyang.com/XML/DTD-Validation-of-XML-with-DTD-Using-DOM.html

    【讨论】:

    • 是的,也许我会尝试一下,谢谢。到目前为止,我只找到了一个通用命令工具,但它只是给了我“错误”而没有它在哪里。谢谢
    • @Tobia 如果您共享您的 xml,dtd 我可以提供帮助并且更具体
    • 我可以使用这个验证,我发现我的错误谢谢
    猜你喜欢
    • 2012-11-28
    • 1970-01-01
    • 2012-08-11
    • 2010-11-22
    • 2012-08-21
    • 2012-09-21
    • 1970-01-01
    • 2014-02-27
    • 2012-05-05
    相关资源
    最近更新 更多