bodong_chen  
Java Space
日历
<2006年4月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
统计
  • 随笔 - 11
  • 文章 - 3
  • 评论 - 2
  • 引用 - 0

导航

常用链接

留言簿(2)

随笔分类

随笔档案

文章档案

Log4j

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 
在一个有关Jdom的讨论中提到:

Elliotte Rusty Harold: XML documents must be well-formed. There are, depending on how you count, anywhere from a hundred to several thousand different rules. These "well-formedness" rules are the minimum requirements for an XML document. The rules cover things like what characters are allowed in element names: The letter 'a' is OK. The letter omega is OK. The asterisk character is not OK. White space is not OK. The rules say that every start-tag has to have a matching end-tag. Elements can nest, but they cannot overlap. Processing instructions have the form <, ?, a target, white space, the data, ?, and a >. Comments cannot contain a double hyphen. There are many such rules governing well-formedness of XML documents.

Validity talks about which elements and attributes are allowed where. Well-formedness only talks about the structure of any XML document, irrespective of what the names are. Validity says, we're only going to allow these elements with these names in these positions. Validity is not required. Well-formedness is.

JDOM, and for that matter DOM, allows you to create malformed documents. They do not check everything they can possibly check. For instance, they do not currently check that the text content of a text node does not contain the null character, which is completely illegal in an XML document.

这是发生在2003年的讨论:http://www.artima.com/intv/jdom2.html。

现在,使用JDOM可以完成这两件事:
1. 验证XML文件的Wellformedness:
The build() method of SAXBuilder throws an IOException if an I/O error such as a broken socket prevents the document from being completely read. It throws a JDOMException if the document is malformed. This is the generic superclass for most things that can go wrong while working with JDOM other than I/O errors.
Example:
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import java.io.IOException;


public class JDOMChecker {

  
public static void main(String[] args) {
  
    
if (args.length == 0) {
      System.out.println(
"Usage: java JDOMChecker URL"); 
      
return;
    } 
      
    SAXBuilder builder 
= new SAXBuilder();
     
    
// command line should offer URIs or file names
    try {
      builder.build(args[
0]);
      
// If there are no well-formedness errors, 
      
// then no exception is thrown
      System.out.println(args[0+ " is well-formed.");
    }
    
// indicates a well-formedness error
    catch (JDOMException e) { 
      System.out.println(args[
0+ " is not well-formed.");
      System.out.println(e.getMessage());
    }  
    
catch (IOException e) { 
      System.out.println(
"Could not check " + args[0]);
      System.out.println(
" because " + e.getMessage());
    } 
  }
}
2. 验证XML文件的Validity:
SAXBuilder only checks documents for well-formedness, not validity. If you want to validate as well, then pass the boolean true to the SAXBuilder() constructor. Then any validity errors will also cause JDOMExceptions.
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import java.io.IOException;


public class JDOMValidator {

  
public static void main(String[] args) {
  
    
if (args.length == 0) {
      System.out.println(
"Usage: java JDOMValidator URL"); 
      
return;
    } 
      
    SAXBuilder builder 
= new SAXBuilder(true);
                                    
//  ^^^^
                                    
// Turn on validation
     
    
// command line should offer URIs or file names
    try {
      builder.build(args[
0]);
      
// If there are no well-formedness or validity errors, 
      
// then no exception is thrown.
      System.out.println(args[0+ " is valid.");
    }
    
// indicates a well-formedness or validity error
    catch (JDOMException e) { 
      System.out.println(args[
0+ " is not valid.");
      System.out.println(e.getMessage());
    }  
    
catch (IOException e) { 
      System.out.println(
"Could not check " + args[0]);
      System.out.println(
" because " + e.getMessage());
    }  
  }
}

参考文献:http://www.ibiblio.org/xml/books/xmljava/chapters/ch14s07.html
posted on 2006-04-03 17:41 bodong_chen 阅读(328) 评论(0)  编辑  收藏 所属分类: JavaXML

只有注册用户登录后才能发表评论。


网站导航:
 
 
Copyright © bodong_chen Powered by: 博客园 模板提供:沪江博客