why do I receive an empty document [#document: null] when I read a xml file with xmlread?

조회 수: 90 (최근 30일)
I tried to read a simple xml file {<rows> <row id='0'> <cell>65</cell> </row> <row id='1'> <cell>565</cell> </row> <row id='2'> <cell>6565</cell> </row></rows>} and received [#document: null].

채택된 답변

Robert Snoeberger
Robert Snoeberger 2015년 12월 19일
편집: Robert Snoeberger 2015년 12월 19일
I'm not sure why you think that you received an empty document.
The display that you are seeing, [#document: null], consists of two parts. The first part is #document, which is the node name. When you parse XML with xmlread, you always receive a #document node as the top-level node.
>> dom = xmlread('example.xml')
dom =
[#document: null]
>> getNodeName(dom)
ans =
#document
>>
The second part is null, which is the value of the node. null is used to indicate that the node doesn't have a value.
>> getNodeValue(dom)
ans =
[]
>>
A #text node usually has a value. In your example, the text node under the first cell element has a value of '65'.
>> cells = dom.getElementsByTagName('cell');
>> cell = cells.item(0)
cell =
[cell: null]
>> text = cell.item(0)
text =
[#text: 65]
>> getNodeValue(text)
ans =
65
>>
  댓글 수: 4
Runzhi Jiao
Runzhi Jiao 2020년 7월 11일
Very good answer! a lot help for me! thanks a lot!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structured Data and XML Documents에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by