|
|
xmlread problems
I am using the xmlread function to parse an xml file. The file has the following structure:
<Parent1>
<Child1>
<Data1>1</Data1>
<Data2>2</Data2>
</Child1>
<Child2>
<Data1>1</Data1>
<Data2>2</Data2>
</Child2>
</Parent1>
I use the xmlread function as follows:
data = xmlread('xmlfile.xml');
Originally, the following is true:
data.item(0) == Parent1
data.item(0).item(0) == Child1
data.item(0).item(1) == Child2
data.item(0).getLength == 2
However, when I open the xml file in XML Notepad and click save (without touching anything else in the file), the next time I try to parse the file I get:
data.item(0) == Parent1
data.item(0).item(0) == [#text:]
data.item(0).item(1) == Child1
data.item(0).item(2) == [#text:]
data.item(0).item(3) == Child2
data.item(0).item(4) == [#text:]
data.item(0).getLength == 5
Any ideas as to what is going on? Any and all help is appreciated.
Thanks.
|
|
0
|
|
|
|
Reply
|
Steven
|
3/8/2010 5:39:05 PM |
|
Sorry to bother you but I have the exact same problem but on my original xml file. Did you ever find an answer? It's fairly easy to resolve by removing all those #text entries in a loop but very annoying.
Thanks
Philippe
|
|
0
|
|
|
|
Reply
|
plumelunaire
|
6/24/2011 6:28:02 PM
|
|
Well, I discovered that the "#text" items were due to whitespace characters in the xml file. For my case, they were newline characters. If I remember correctly (it's been a while), the original xml file did not contain newline characters in it. That is, if you opened it in a text editor, it was all on one line. When I would re-save the file using XML Editor, it would format the file so that when you opened it in a text editor, it would appear formatted properly. That is, it would appear on multiple lines.
I wrote the following function to skip the items that were "#text".
===========================================
%% skipItem Subfunction
% Returns the item number of the next child that is not a newline or
% whitespace child, after start_num. Returns 'none' if there are no
% more valid item numbers.
function item_num = skipItem(parent,start_num)
item_num = start_num;
if isempty(parent.item(item_num))
item_num = 'none';
return
end
item_name = parent.item(item_num).getNodeName;
while strcmp(item_name,'#text')
item_num = item_num + 1;
if isempty(parent.item(item_num))
item_num = 'none';
return
end
item_name = parent.item(item_num).getNodeName;
end
end
===========================================
Here is an example of how to use this function:
data = xmlread(filename);
parent = data.item(0);
child_item = skipItem(parent,0)
child = parent.item(child_item);
I hope that helps. Let me know if you have other questions.
|
|
0
|
|
|
|
Reply
|
noemail4 (15)
|
6/24/2011 7:22:05 PM
|
|
|
2 Replies
274 Views
(page loaded in 0.046 seconds)
|
|
|
|
|
|
|
|
|