以前经常会在List的index处理上犯微小的错误,或大一,或小一,但结果都是一样:wrong!所以在这里简要总结一下最容易出错的两个方法。
1. List的indexOf(Object obj)方法:
这个方法返回的是List中一个节点的序号。例如:第一个节点返回0,最后一个节点返回list.size()-1。
2. List的subList(int begin, int end)方法:
这个方法是取出一个List的一个子List,在两个参数上很容易犯错误。
以一个size为5的List为例:
subList(0, 2);取出的是第一个到第二个节点;
subList(0, 1);取出第一个节点;
subList(1, 1);不取出任何节点;
subList(2, 1);则会出参数错误的异常。
可以看出,begin为起始节点的序号,end为所要取的最后一个节点的序号+1。
jdk中的这些类有很多细节都需要非常注意,不然实际的coding中会遇到很多麻烦。
在一个有关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