在xml文件中使用xmlread搜索文本 [英] search a text using xmlread in xml file

查看:85
本文介绍了在xml文件中使用xmlread搜索文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在xml文件中搜索文本并检查其值是否正确 使用Matlab.

I would like to search a text in xml file and check that it has the correct value using Matlab.

我尝试过:

myFolder = 'folder1';
OutputFile = fullfile(myFolder ,'info.xml');
xmlNode = xmlread(OutputFile );

我想检查"characteristic","color"选项是否存在,并且它们分别具有值:hybrid和red?

I would like to check that 'characteristic','color' options exists and that they have respectively the values : hybrid and red ?

info.xml内容--------------

info.xml content--------------

<?xml version="1.0" encoding="utf-8"?>
<Custom_project name="" val="True" name="file1" path="file:/C:/Users/Local/Temp/info.xml" version="1.0">
   <verif="true" name="values" path="file:/C:/Users/Temp/folder1">
      <optList name="values">
         <opt name="color">red</option>
         <opt name="police">calibri</option>
         <opt name="font">blue</option>
      </optList>
    </verif>
   <toto="myvalue" name="option1">
      <opt name="myvalue_1">32</option>
      <opt name="-total">All</option>
      <opt name="characteristic">hybrid</option>
   </toto>

推荐答案

如果不需要使用xmlread,则这是一种可能的解决方案.不会检查同一XML选项的多个定义,因此任何匹配的选项名称-选项值对都算作匹配项.

If using xmlread is not required, this is one possible solution. Multiple definitions of the same XML options are not checked, so any matching option name - option value pair is counted as a match.

%函数.

function [IsMatch] = xmlmatch(XMLdata, Variable, Value)
IsMatch = ~isempty(regexp(XMLdata, [ '.*<opt\s*name="', Variable, '">', Value, '</option>.*' ]));
return

%打开文件,获取文件ID

% open the file, get file ID

fid = fopen('info.xml');

%读取文件内容(假定为文本文件).

% read the file contents (assume text file).

XMLdata = fscanf(fid, '%s');

%检查XMLdata变量的值:

% check the XMLdata's variables' values:

IsColorRed = xmlmatch(XMLdata, 'color', 'red');
IsColorRed =
    1

IsColorBlue = xmlmatch(XMLdata, 'color', 'blue');
IsColorBlue =
    0

IsCharacteristicHybrid = xmlmatch(XMLdata, 'characteristic', 'hybrid');
IsCharacteristicHybrid =
    1

IsCharacteristicHybr = xmlmatch(XMLdata, 'characteristic', 'hybr');
IsCharacteristicHybr =
    0

%关闭文件.

fclose(fid);

这篇关于在xml文件中使用xmlread搜索文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆