[xml] XmlTextReader vs, <elem />



Hi!

I am having problem with XmlTextReader, if my XML file has elements in the form of <name atr1="value1" atr2="value2" ... /> or even just <name />: I just can't detect the end of such an element. I am reading the XML file in a loop, in which I call xmlTextReaderRead followed by xmlTextReaderNodeType, and after that I process the node according to its type. My problem is that I have two situations which are completely different semantically but get the same result from XmlTextReader:

Situation A:
<root>
  <elem1 />
  <elem2>...</elem2>
</root

Situation B:
<root>
  <elem1>
    <elem2>...</elem2>
  </elem1>
</root

The output of xmlTextReaderNodeType for situation A:
- XML_READER_TYPE_ELEMENT (<root>)
- XML_READER_TYPE_SIGNIFICANT_WHITESPACE (end of line, indent)
- XML_READER_TYPE_ELEMENT (<elem1>)
- XML_READER_TYPE_SIGNIFICANT_WHITESPACE (end of line, indent)
- XML_READER_TYPE_ELEMENT (<elem2>)
- XML_READER_TYPE_SIGNIFICANT_WHITESPACE (end of line, indent)
- XML_READER_TYPE_END_ELEMENT (end of <elem2>)
- XML_READER_TYPE_SIGNIFICANT_WHITESPACE (end of line, indent)
- XML_READER_TYPE_END_ELEMENT (end of <root>)
- XML_READER_TYPE_SIGNIFICANT_WHITESPACE (end of line, indent)

The output of xmlTextReaderNodeType for situation B:
- XML_READER_TYPE_ELEMENT (<root>)
- XML_READER_TYPE_SIGNIFICANT_WHITESPACE (end of line, indent)
- XML_READER_TYPE_ELEMENT (<elem1>)
- XML_READER_TYPE_SIGNIFICANT_WHITESPACE (end of line, indent)
- XML_READER_TYPE_ELEMENT (<elem2>)
- XML_READER_TYPE_SIGNIFICANT_WHITESPACE (end of line, indent)
- XML_READER_TYPE_END_ELEMENT (end of <elem2>)
- XML_READER_TYPE_SIGNIFICANT_WHITESPACE (end of line, indent)
- XML_READER_TYPE_END_ELEMENT (end of <elem1>)
- XML_READER_TYPE_SIGNIFICANT_WHITESPACE (end of line, indent)
- XML_READER_TYPE_END_ELEMENT (end of <root>)
- XML_READER_TYPE_SIGNIFICANT_WHITESPACE (end of line, indent)

My problem is that in the first case I can't tell whether the last XML_READER_TYPE_END_ELEMENT applies to <root> or to <elem1>. I tried experimenting with xmlTextReaderHasValue and xmlTextReaderIsEmptyElement, but neither seems to produce usable results in these situations. The only solution seems to be using xmlTextReaderDepth, but that does seem like a rather dirty way of detecting end of element. Is there any particular reason why won't libxml2 send me XML_READER_TYPE_END_ELEMENT when it encounters the /> part of <elem />?

Thanks,

Pepak



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]