1

我正在尝试使用dom包解析 XML 文件,但这是我得到的错误:

unterminatedattribute {invalid attribute list around line 4}

这是一个简单的测试:

 package require dom;
 set XML "
    <Top>
    <Name name='name' />
    <Group number=1>
    <Member name='name1' test='test1' l=100/>
    </Group>
    </Top>"
set doc [::dom::parse $XML]

set root [$doc cget -documentElement]

set node [$root cget -firstChild]
puts "[$node cget -nodeValue]"
4

2 回答 2

3

“XML”实际上在形式上是无效的;必须引用所有属性值。如果可以,请修复它。

set XML "
    <Top>
    <Name name='name' />
    <Group number='1'>
    <Member name='name1' test='test1' l='100'/>
    </Group>
    </Top>"

If you can't fix that, you might try using tDOM instead in HTML mode (which is a lot laxer about well-formedness constraints, though it also lower-cases all element and attribute names). Mind you, even with that it still fails on your particular input document:

% package require tdom
0.8.3
% set doc [dom parse -html $XML]
error "Unterminated element 'group' (within 'member')" at position 114
">
    <group number=1>
    <member name='name1' test='test1' l=100/>
    </group> <--Error-- 
    </Top>"

Fixing your document is the #1 thing to do!

于 2013-07-16T08:01:50.533 回答
2

问题是您必须用"or将元素值括起来'。修复 XML 后,解析成功。

我通常不使用 dom 包,而是使用tdom包。
tdom 包有一个-html启用松散解析的选项。

于 2013-07-16T07:55:12.587 回答