【发布时间】:2019-09-23 14:15:35
【问题描述】:
我有 XML 文件要由 java sax 解析器解析。我希望解析器只解析开始和结束标记(<Models> 和</Models>)之间的内容,而不是整个文件。如何指定要由 java Sax Parser 解析的 XML 文件的一部分?
我的xml文件如下:
some tags
<Models>
my content to be parsed
<LifeLine...
<state condition...
other tags
</LifeLine>
......
</Models>
<Diagrams>
....
<LifeLine..
<state condition...
<State condition..
</LifeLine>
other tags
</Diagrams>
所以我希望 Sax Parser 仅解析 <Models> 和 </Models> 之间的内容,因为在解析整个文件中 statecondition 的值时,输出会为我提供 部分中的 State condition 值我不想读它们。
有什么帮助吗?
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equals("LifeLine")) {
currentLifeLine=new Lifeline(attributes.getValue("Name"), StateList,TimeInstList);
}
else if (qName.equals("StateCondition")) {
currentSC=new State(attributes.getValue("Id"), attributes.getValue("Name"));
}
.................
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("StateCondition")) {
currentLifeLine.addStateCondition(currentSC);}...
【问题讨论】:
-
将
Models的开头存储在一个布尔变量中,如果该布尔变量为真,则只读出LifeLine,当然当你得到@987654330时在endElement处理程序中将它设置为假@在那里报道。 -
@Martin Honnen 谢谢