XML: Count how many <Tags>

조회 수: 14 (최근 30일)
Nycholas Maia
Nycholas Maia 2019년 1월 3일
댓글: Sean de Wolski 2019년 1월 7일
I have a big XML file and I would like to count how many a specific tag 'X' appears in this text file.
Example: In this example I have a total of 4 X tags in the whole XML file
%% === HERE I HAVE 3 'X Tags' in the XML Level 3 ===
<A> % Level 1
<B> % Level 2
<X> % Level 3
"Some text 1"
</X>
<X> % Level 3
"Some text 2"
</X>
<X> % Level 3
"Some text 3"
</X>
</ B>
</A>
%% === HERE I HAVE ONLY 1 'X Tag' in the XML Level 3 ===
<A> % Level 1
<B> % Level 2
<X> % Level 3
"Some text 4"
</X>
</ B>
</A>
%% === HERE I DON'T HAVE ANY 'X Tag' in the XML Level 3, AND THE LEVEL 3 DOESN'T EXISTS TOO ===
<A> % Level 1
<B> % Level 2
</ B>
</A>
How can I count the total amount of 'X' tags and get a good performace and small time to compute it?

채택된 답변

Sean de Wolski
Sean de Wolski 2019년 1월 3일
Something along these lines:
xml = xmlread(xmlfile);
Xtag = xml.getElementsByTagName('X');
Xtag.getLength
  댓글 수: 4
Nycholas Maia
Nycholas Maia 2019년 1월 4일
Now, I got the correct numbers using this:
text = fileread('my_file.xml');
num_parts = length(strfind(text, '</score-part>'));
num_measures = length(strfind(text, '</measure>'));
num_notes = length(strfind(text, '</note>'));
I don't know if this is the best way thinking about computation performace, but it's working for now...
If there is a better way to do this, please, comment!
Thank you!
Sean de Wolski
Sean de Wolski 2019년 1월 7일
s = string(fileread(xmlfile))
xs = extractBetween(s, "<x", "</x>")
numel(xs)
Or similar.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Direction of Arrival Estimation에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by