代码之家  ›  专栏  ›  技术社区  ›  Tobia

JaxB类的DTD验证

  •  0
  • Tobia  · 技术社区  · 6 年前

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

    我使用了jxc实用程序:

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

    我可以生成xml,但是它被接收者拒绝了,除了这个DTD,我没有其他信息。如何根据XML验证DTD?

    0 回复  |  直到 6 年前
        1
  •  1
  •   martidis    6 年前

    下面是一些针对xml验证DTD的示例。为了方便,我也会贴在这里。

    对于包含dtd的给定xml,无效_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

    推荐文章