2011-07-15 36 views
5

Soy un principiante en la programación completa tratando de aprender MATLAB. Quiero extraer datos numéricos de un grupo de diferentes archivos xml. Los elementos de datos numéricos están delimitados por las etiquetas y. ¿Cómo escribo un programa en MATLAB?extrayendo datos de archivos xml usando MATLAB

Mi algoritmo:

1. Open the folder 
2. Look into each of 50 xml files, one at a time 
3. Where the tag <HNB.1></HNB.1> exists, copy numerical contents between said tag and write results into a new file 
4. The new file name given for step 3 should be the same as the initial file name read in Step 2, being appended with "_data extracted" 

ejemplo:

FileName = Stewart.xml 
Contents = blah blah blah <HNB.1>2</HNB.1> blah blah 
NewFileName = Stewart_data extracted.txt 
Contents = 2 
+0

posible duplicado: http: // stackoverflow .com/questions/6582250/extracting-data-between-two-tags-in-html-file-matlab – Amro

Respuesta

8

La función fundamental en MATLAB para leer datos XML es xmlread; pero si eres un principiante completo, puede ser complicado trabajar con eso. Pruebe this series of blog postings que le muestran cómo ponerlo todo junto.

1

Supongamos que desea leer este archivo:

<PositiveSamples numImages="14"> 
 
<image numSubRegions="2" filename="TestingScene.jpg"> 
 
\t <subregion yStart="213" yEnd="683" xStart="1" xEnd="236"/> 
 
\t <subregion yStart="196" yEnd="518" xStart="65" xEnd="226"/> 
 
</image> 
 
</PositiveSamples>

Luego, en Matlab, lee el contenido del archivo de la siguiente manera:

%read xml file 
xmlDoc = xmlread('PositiveSamples.xml'); 

%Get root element 
root = xmlDoc.getDocumentElement(); 

%Read attributevale 
numOfImages = root.getAttribute('numImages'); 
numOfImages = char(numImages); 
numOfImages = uint16(eval(numImages));