admin管理员组

文章数量:1530522

2024年7月30日发(作者:)

Matlab自带了xmlread,xmlwrite等三个与xml相关的函 数,但是使用起来仍

然不方便。

在Matlab主页上有个xml_io_tools的工具,很不错,简单易用,一个小时基本

就可以掌握了。

简单记录一下笔记:

1、ine = 'Xerces'

Same operation using Apache Xerces XML engine. But root element name

was passed as variable

2、"Item" flag in "xml_write" true false

保存结构

true 自动加入

false 保持原样

read时结果会保持一致的

存在一个潜在的问题,这是由系统决定,注意避免即可

3、"em" flag in "xml_write" true false

保持cell

与2有相同做法

注意cellitem可以控制cell/struct结构储存

4、"s" flag in "xml_read"

false 读出结构体

true 将结构体进行混合

5、"me" flag in "xml_write"

控制自动加入的'item'

6、"me" flag in "xml_read"

读取自动控制

6、"2Num" flag in xml_read

"2Num = false" to keep all the fields in string format

机构不肯能完全保证一致:WARNING

Notice that both settings will produce the same output for "num1" and

"num2" and there is no way to reproduce the original "MyTree" structure.

7、Use ATTRIBUTE and CONTENT

MyTree=[];

er = 13;

T = 'Hello World';

= 2;

xml_write('', MyTree);

type('')

8、Use Comments

MyTree=[];

T = 'This is a comment';

er = 13;

T = 'Hello World';

xml_write('', MyTree);

type('')

9、Comments in XML top level (method #1) This method uses cell array

MyTree=[];

er = 13;

ng = 'Hello World';

xml_write('', MyTree, {'MyTree', [], 'This is a global comment'});

type('')

10、Comments in XML top level (method #2)

"ly = false",

MyTree=[];

T = 'This is a global comment';

er = 13;

ng = 'Hello World';

Pref=[]; ly = false;

xml_write('', MyTree, [], Pref);

type('')

11、Write XML files with PROCESSING_INSTRUCTIONs

MyTree=[];

SING_INSTRUCTION = 'xml-stylesheet type="a" href="foo"';

er = 13;

ng = 'Hello World';

xml_write('', MyTree);

type('')

12、Write XML files with CDATA Sections

Same operation using Apache Xerces XML engine produces correct results

13、Write XML files with special characters in TAG names

genvarname('My-Number')

Num_0xF6

Also two of the characters '-' and ':' can be encoded by a special strings:

'_DASH_' and '_COLON_' respectively

0x3A=:

名域必须先被加上

14、Read only the Root Element (default)

15、"tr" flag in "xml_read"

16、Using "ec" flag in "xml_read"

17、"ly" flag in "xml_read"As it was shown in previous examples

RootOnly parameter can be used to capture global (top level) special nodes

(like COMMENTs and PROCESSING_INSTRUCTIONs) which are ignored by default

18、"ly" flag in "xml_write" gives wrong output file

19、"els" flag in "xml_read"

Use Default ("els = infinity") setting

20、Create DOM object based on a Struct using "xml_write"

Create Struct tree

MyTree=[];

er = 13;

ng = 'Hello World';

Convert Struct to DOM object using xml_write

DOM = xml_write([], MyTree);

xmlwrite('', DOM); % Save DOM object using MATLAB function

type('')

Convert DOM object to Struct using "xml_read"DOM =

xmlread(''); % Read DOM object using MATLAB function

[tree treeName] = xml_read(DOM); % Convert DOM object to Struct

disp([treeName ' ='])

gen_object_display(tree)

Write XML file based on a DOM using

"xml_write_xerces"xmlwrite_xerces('', DOM); % Save DOM object

using Xerces library

type('')

Matlab 7.0 中XML文件读写测试

Matlab 7.0 中对xml的读写操作主要使用到 [toolboxmatlabiofun] 工具包中的

xmlread.m 和 xmlwrite.m 文件。使用XML Document Object Model的方法,实现对xml文

件的操作。Matlab中的xml操作实际上采用了Java中的DOM的方法。

读XML: (MATLAB701安安装路径下)toolboxmatlabiofun 的 xmlread.m

XMLREAD [转换一个XML文档,返回一个Document Object Model节点。]

DOMNODE = XMLREAD(FILENAME) 读取一个文件名或XML的URL,

并对文档转换返回一个Document Object Model节点。

写XML: (MATLAB701安安装路径下)toolboxmatlabiofun 的 xmlwrite.m

XMLWRITE [序列化一个 XML Document Object Model 节点成为一个 输出文件。]

XMLWRITE(FILENAME,DOMNODE) 序列化 DOMNODE 成为

FILENAME 所指定的文件。

S = XMLWRITE(DOMNODE) 以字符串的方式返回这个XML节点树。

在Matlab m文件编辑器中编写需要用到XML文件的m文件时,由于Matlab环境路径

变量的设置,测试运行时不需要把xmlread.m和xmlwrite.m拷贝到同一路径。但是需要作成

COM组件时,需要把这两个文件和用户作成的m文件一起添加进COM作成工程。编译后

的dll才能够实现对xml文件的读写操作。

对XML Document Object Model 更多的了解,可参照 W3C Document Object Model

(DOM):/DOM/。

对JAVA中使用DOM的方法,可参考Sun Web site:/xml/docs/api。

程序示例:

M文件 (

xmlwritetest1.m

):

function xmlwritetest1(infilename,outfilename)

tic; % 开始计时

%-----------------IN XML--------------------------------------------%

try

xDoc = xmlread(infilename);

catch

error('Failed to read XML file %s.',infilename);

end

allSylListItems = mentsByTagName('syl');

intCount = 0;

for k = 0:gth-1

thisListItem = (k);

childNode = stChild;

while ~isempty(childNode)

if eType == T_NODE

childText = char(a);

switch char(Name)

case 'rm';

intCount = intCount + 1;

rm(intCount) = str2double(char(childText));

case 'rp' ;

rp(intCount) = str2double(char(childText));

end

end

childNode = tSibling;

end

end

%-----------------Process--------------------------------------------%

% 转为列向量

rp = rp(:);

rm = rm(:);

%获取样本数

n=length(rp);

if length(rm) ~= n

ReturnCode = 1

ReturnMsg = 'Rp,Rm样本数据维数不一致';

else

ReturnCode = 0

ReturnMsg = '最终结果';

outBETA = beta(rp,rm);

end

Runtime = toc; %执行时间

%-----------------OUT XML--------------------------------------------%

% 创建 XML document.

docNode = Document('outBETA');

docRootNode = umentElement;

%添加结果数据

if ReturnCode == 0

for i=1:length(outBETA)

thisElement = Element('Beta');

(TextNode(sprintf('%f',outBETA(i))));

Child(thisElement);

end

end

%添加返回值

%结果代码

CodeElement = Element('ReturnInfo');

(TextNode(sprintf('%d',ReturnCode)));

Child(CodeElement);

%结果信息

MsgElement = Element('ReturnMsg');

(TextNode(sprintf('%s',ReturnMsg)));

Child(MsgElement);

XMLREAD Parse an XML document and return a Document Object Model node.

DOMNODE = XMLREAD(FILENAME) reads a URL or file name in the

string input argument FILENAME. The function returns DOMNODE,

a Document Object Model (DOM) node representing the parsed document.

The node can be manipulated by using standard DOM functions.

Note: A properly parsed document will display to the screen as

>> xDoc = xmlread(...)

xDoc =

[#document: null]

Example 1: All XML files have a single root element. Some XML

files declare a preferred schema file as an attribute of this element.

xDoc = xmlread(fullfile(matlabroot,'toolbox/matlab/general/'));

xRoot = umentElement;

schemaURL = char(ribute('xsi:noNamespaceSchemaLocation'))

Example 2: Each file on the MATLAB path contains

several elements with a

script finds the callback that corresponds to the label 'Plot Tools'.

infoLabel = 'Plot Tools'; infoCbk = ''; itemFound = false;

xDoc = xmlread(fullfile(matlabroot,'toolbox/matlab/general/'));

% Find a deep list of all elements.

allListItems = mentsByTagName('listitem');

%Note that the item list index is zero-based.

for i=0:gth-1

thisListItem = (i);

childNode = stChild;

while ~isempty(childNode)

%Filter out text, comments, and processing instructions.

if eType == T_NODE

%Assume that each element has a single child

childText = char(a);

switch char(Name)

case 'label' ; itemFound = strcmp(childText,infoLabel);

case 'callback' ; infoCbk = childText;

end

end

childNode = tSibling;

end

if itemFound break; else infoCbk = ''; end

end

disp(sprintf('Item "%s" has a callback of "%s".',infoLabel,infoCbk))

See also xmlwrite, xslt.

Reference page in Help browser

doc xmlread

建议你多用下help,.

本文标签: 文件结构需要结果作成