编辑:这段代码没问题。我在某处发现了我的伪代码中不存在的逻辑错误。我将其归咎于我缺乏 Java 经验。
在下面的伪代码 中,我试图解析显示的 XML。也许是一个愚蠢的例子,但我的代码太大/太具体,任何人都无法从看到它和从发布的答案中学习中获得任何真正的值(value)。所以,这更有趣,希望其他人和我一样可以从答案中学习。
我是 Java 的新手,但我是一名经验丰富的 C++ 程序员,这让我相信我的问题在于我对 Java 语言的理解。
问题:解析器完成后,我的 Vector 充满了未初始化的 Cow。我创建了具有默认容量的 Cows Vector(如果它类似于 C++ STL Vector,则不应影响它的“大小”)。当我在解析后打印出 Cow Vector 的内容时,它给出了正确的 Vector 大小,但所有值似乎从未设置过。
信息:我已经使用其他没有 Vector 字段 的解析器成功地做到了这一点,但在这种情况下,我想使用 Vector 来累积 Cow 属性。
更多信息:我无法使用泛型 (Vector< Cow >),所以请不要指指点点。 :)
提前致谢。
<pluralcow>
<cow>
<color>black</color>
<age>1</age>
</cow>
<cow>
<color>brown</color>
<age>2</age>
</cow>
<cow>
<color>blue</color>
<age>3</age>
</cow>
</pluralcow>
public class Handler extends DefaultHandler{
// vector to store all the cow knowledge
private Vector m_CowVec;
// temp variable to store cow knowledge until
// we're ready to add it to the vector
private Cow m_WorkingCow;
// flags to indicate when to look at char data
private boolean m_bColor;
private boolean m_bAge;
public void startElement(...tag...)
{
if(tag == pluralcow){ // rule: there is only 1 pluralcow tag in the doc
// I happen to magically know how many cows there are here.
m_CowVec = new Vector(numcows);
}else if(tag == cow ){ // rule: multiple cow tags exist
m_WorkingCow = new Cow();
}else if(tag == color){ // rule: single color within cow
m_bColor = true;
}else if(tag == age){ // rule: single age within cow
m_bAge = true;
}
}
public void characters(...chars...)
{
if(m_bColor){
m_WorkingCow.setColor(chars);
}else if(m_bAge){
m_WorkingCow.setAge(chars);
}
}
public void endElement(...tag...)
{
if(tag == pluralcow){
// that's all the cows
}else if(tag == cow ){
m_CowVec.addElement(m_WorkingCow);
}else if(tag == color){
m_bColor = false;
}else if(tag == age){
m_bAge = false;
}
}
}
最佳答案
当您说 Cows 未初始化时,String 属性是否已初始化为 null?还是空字符串?
我知道你提到这是伪代码,但我只是想指出几个潜在的问题:
public void startElement(...tag...)
{
if(tag == pluralcow){ // rule: there is only 1 pluralcow tag in the doc
// I happen to magically know how many cows there are here.
m_CowVec = new Vector(numcows);
}else if(tag == cow ){ // rule: multiple cow tags exist
m_WorkingCow = new Cow();
}else if(tag == color){ // rule: single color within cow
m_bColor = true;
}else if(tag == age){ // rule: single age within cow
m_bAge = true;
}
}
你真的应该在这里使用 tag.equals(...) 而不是 tag == ...。
public void characters(...chars...)
{
if(m_bColor){
m_WorkingCow.setColor(chars);
}else if(m_bAge){
m_WorkingCow.setAge(chars);
}
}
我假设您已经意识到这一点,但实际上调用此方法时使用的是带有开始索引和结束索引的字符缓冲区。
另请注意,characters(...) 可以为单个文本 block 调用多次,每次调用都返回小块: http://java.sun.com/j2se/1.4.2/docs/api/org/xml/sax/ContentHandler.html#characters(char[],%20int,%20int)
"...SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks..."
我怀疑您会在您提供的简单示例中遇到该问题,但您还提到这是一个更复杂问题的简化版本。如果在您的原始问题中,您的 XML 包含大文本 block ,则需要考虑这一点。
最后,正如其他人所提到的,如果可以的话,考虑使用 XML 编码库(例如 JAXB、Castor、JIBX、XMLBeans、XStream 等等)是个好主意。
关于Java Vector Field(私有(private)成员)累加器不存储我的奶牛!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/237561/