matlab的xml工具箱docxWord下载.docx
- 文档编号:17483898
- 上传时间:2022-12-06
- 格式:DOCX
- 页数:12
- 大小:30.19KB
matlab的xml工具箱docxWord下载.docx
《matlab的xml工具箱docxWord下载.docx》由会员分享,可在线阅读,更多相关《matlab的xml工具箱docxWord下载.docx(12页珍藏版)》请在冰豆网上搜索。
num2"
andthereisnowaytoreproducetheoriginal"
MyTree"
structure.
7、UseATTRIBUTEandCONTENT
MyTree=[];
MyTree.MyNumber=13;
MyTree.MyString.CONTENT='
HelloWorld'
;
MyTree.MyString.ATTRIBUTE.Num=2;
xml_write('
test.xml'
MyTree);
type('
)
8、UseComments
MyTree.COMMENT='
Thisisacomment'
MyTree.MyString.CONTENT='
9、CommentsinXMLtoplevel(method#1)Thismethodusescellarray
MyTree.MyString='
xml_write('
MyTree,{'
MyTree'
[],'
Thisisaglobalcomment'
});
type('
10、CommentsinXMLtoplevel(method#2)
Pref.RootOnly=false"
MyTree.MyTest.MyNumber=13;
MyTree.MyTest.MyString='
Pref=[];
Pref.RootOnly=false;
MyTree,[],Pref);
11、WriteXMLfileswithPROCESSING_INSTRUCTIONs
MyTree.PROCESSING_INSTRUCTION='
xml-stylesheettype="
a"
href="
foo"
'
12、WriteXMLfileswithCDATASections
SameoperationusingApacheXercesXMLengineproducescorrectresults
13、WriteXMLfileswithspecialcharactersinTAGnamesgenvarname('
My-Number'
Num_0xF6
Alsotwoofthecharacters'
-'
and'
:
canbeencodedbyaspecialstrings:
'
_DASH_'
_COLON_'
respectively
0x3A=:
名域必须先被加上
14、ReadonlytheRootElement(default)
15、"
Pref.ReadAttr"
16、Using"
Pref.ReadSpec"
17、"
Pref.RootOnly"
AsitwasshowninpreviousexamplesRootOnlyparametercanbeusedtocaptureglobal(toplevel)specialnodes
(likeCOMMENTasndPROCESSING_INSTRUCTIOwNhsi)chareignoredbydefault
18、"
giveswrongoutputfile
19、"
Pref.NumLevels"
UseDefault("
Pref.NumLevels=infinity"
)setting
20、CreateDOMobjectbasedonaStructusing"
CreateStructtreeMyTree=[];
MyTree.MyString='
ConvertStructtoDOMobjectusingxml_writeDOM=xml_write([],MyTree);
xmlwrite('
DOM);
%SaveDOMobjectusingMATLABfunctiontype('
ConvertDOMobjecttoStructusing"
DOM=xmlread('
);
%ReadDOMobjectusingMATLABfunction[treetreeName]=xml_read(DOM);
%ConvertDOMobjecttoStructdisp([treeName'
='
])gen_object_display(tree)
WriteXMLfilebasedonaDOMusing"
xml_write_xerces"
xmlwrite_xerces('
%SaveDOMobjectusingXerceslibrarytype('
Matlab7.0中XML文件读写测试
Matlab7.0
中对xml的读写操作主要使用到[toolbox\matlab\iofun]工具包中的
xmlread.m和xmlwrite.m文件。
使用XMLDocumentObjectModel的方法,实现对xml文件的操作。
Matlab中的xml操作实际上采用了Java中的DOM的方法。
读XML:
(MATLAB701安安装路径下)\toolbox\matlab\iofun的xmlread.m
XMLREAD
[转换一个XML文档,返回一个DocumentObjectModel节点。
]
DOMNODE=XMLREAD(FILENAME)读取一个文件名或XML的URL,并对文档转换返回一个DocumentObjectModel节点。
写XML:
(MATLAB701安安装路径下)\toolbox\matlab\iofun的xmlwrite.m
XMLWRITE[序列化一个XMLDocumentObjectModel节点成为一个输出文件。
]
XMLWRITE(FILENAME,DOMNODE)序列化DOMNODE成为FILENAME所指定的文件。
S=XMLWRITE(DOMNODE)以字符串的方式返回这个XML节点树。
在Matlabm文件编辑器中编写需要用到XML文件的m文件时,由于Matlab环境路径变量的设置,测试运行时不需要把xmlread.m和xmlwrite.m拷贝到同一路径。
但是需要作成COM组件时,需要把这两个文件和用户作成的m文件一起添加进COM作成工程。
编译后
的dll才能够实现对xml文件的读写操作。
对XMLDocumentObjectModel更多的了解,可参照W3CDocumentObjectModel
(DOM):
http:
//www.w3.org/DOM/。
对JAVA中使用DOM的方法,可参考SunWebsite:
程序示例:
M文件(xmlwritetestl.m):
functionxmlwritetest1(infilename,outfilename)
tic;
%开始计时
%INXML%
try
xDoc=xmlread(infilename);
catch
error('
FailedtoreadXMLfile%s.'
infilename);
end
allSylListItems=xDoc.getElementsByTagName('
syl'
intCount=0;
fork=0:
allSylListItems.getLength-1
thisListItem=allSylListItems.item(k);
childNode=thisListItem.getFirstChild;
while~isempty(childNode)
ifchildNode.getNodeType==childNode.ELEMENT_NODEchildText=char(childNode.getFirstChild.getData);
switchchar(childNode.getTagName)
case'
rm'
;
intCount=intCount+1;
rm(intCount)=str2double(char(childText));
rp'
rp(intCount)=str2double(char(childText));
end
endchildNode=childNode.getNextSibling;
%Process%
%转为列向量
rp=rp(:
rm=rm(:
%获取样本数n=length(rp);
iflength(rm)~=n
ReturnCode=1
ReturnMsg='
Rp,Rm样本数据维数不一致'
else
ReturnCode=0
最终结果'
outBETA=beta(rp,rm);
Runtime=toc;
%执行时间
%OUTXML%
%创建XMLdocument.
docNode=com.mathworks.xml.XMLUtils.createDocument('
outBETA'
);
docRootNode=docNode.getDocumentElement;
%添加结果数据ifReturnCode==0fori=1:
length(outBETA)
thisElement=docNode.createElement('
Beta'
thisElement.appendChild...
(docNode.createTextNode(sprintf('
%f'
outBETA(i))));
docRootNode.appendChild(thisElement);
%添加返回值
%结果代码
CodeElement=docNode.createElement('
Returnlnfo'
CodeElement.appendChild...
%d'
ReturnCode)));
docRootNode.appendChild(CodeElement);
%结果信息
MsgElement=docNode.createElement('
ReturnMsg'
MsgElement.appendChild...
%s'
ReturnMsg)));
docRootNode.appendChild(MsgElement);
ihcStruct
Name:
Parameter*
Attributes:
Daw:
M
Children:
Name'
Value:
AdapliveFiller*
<
IxIS/rucZ>
IXIStruct>
IxIStruct>
Ix\Struct>
Name:
粗exT
Atributes:
[]
DalaC•(此处为换行和缩进空格)
Value*
Daia:
^
Chil/irrn•
NameName'
Value:
*Position*
^Jame'
#rex/"
Altrihnles:
|]
NameI'
itlcxt9
DuiaC•(此处为换行和缩进空格)
Afc/me:
*Value*
Do/a:
-
9Name1
Method*
Name'
#text'
凶口
lheStrucI
1x\Struct>
«
*#tc
Attributes
Data:
•
Attribuies:
Data:
V
0
xt*
n
(此处为换行和缩进空格)
/Vame:
Atlrihtttcs
|Numc:
*Name'
[Value:
Method'
Duia:
9'
Name*#/c.v/*
Altribules:
||
DatuCMTF90
►
*#tcxt*
AttributesData:
[1
•(此处为换行和缩进空格)
function
thcStruct
■parscXML(filcnaxic)
%PARSEXKLConvertXMLfiletoa
WATLABstructure・
tree■
xmlread<
filename>
34
errorCFailedtoreadXMLfile%s.\filenairTe);
%Recurseoverchildnodes・Thiscouldrunintoproblems%withverydeeplynestedtrees・
theStruct«
parseChilaNoaes(tree);
Unabletoend
Subfunction
functionchildren■
%Recurseovernode
parseXMLfile,filenarr^);
PARSECHILDNODES-
parseChildNodcs(thcNode)children・
children=[);
ittheNode・hasChlldNodes
childNodes■theNode・gotChi"
Nodes;
nutr.ChildNodcs-chilaNodes.getLcngth;
allocCell=cell(lrnumChildNodes);
children■
struct<
•Data\
forcount
allocCcll/'
AttributesollocCell/allocCell,'
ChildrensallocCell);
■1:
numChilaNodea
thcChild■childNodcs.item(count-1);
children(count)-makeStructFro<
r.No<
ie(theChiLd);
allocColl■cell<
lrnumChildNodcs);
children«
struct<
allocCell,・Attributes•.allocCell,«
llocCollr・Children'
«
11ocC«
1I);
=1:
numCbildNoaes
theChild-childNodes.it已m(count1);
children{count)■makeStructFron-.Node(theChild);
ena
SubfunctionMAKESTRQCTFROMNODE―
functionnodcStruct■makcStructFrocrXodc(thcNodc)
%Createstructureofnodeinfo・
nodeStruct・struct(
zchor<
thcNodc・gctNodcNomc),
•Attributes・•parseAttributes(theNode).
\・・・•Children*rparseChilaNoaes(theNode));
ifany<
stremp(methods(theNodel/'
getData*))nodeStruct.Data=char(theNode.getOBta);
凶侈篦管口
nodeStruct.Data
mttributes■[];
1£
LPieMuderibUiCsS
thcAttributes=thcNddc・QutAttribulicss』nurrALLrLhuLui,_LiseAttribillus.g□E.Lljrt-gLhallcHzCell■cell41TnumAttribateaJ;
flttrib-utes-struct('
K.irnE^R,allQcC-cll^■allocCellf;
farcount«
11numAttributea
attrib■th<
-Attribyte-5-itCCT.Ccount-L);
attributes1count-Name二char(attiLb.getName);
SttrLbutSS1count|i,Vfllu-ifl-=ch®
rtsttrLbBg«
tV#lus)j
XMLREADParseanXMLdocumentandreturnaDocumentObjectModelnode.
DOMNODE=XMLREAD(FILENAME)readsaURLorfilenameinthestringinputargumentFILENAME.ThefunctionreturnsDOMNODE,aDocumentObjectModel(DOM)noderepresentingtheparseddocument.ThenodecanbemanipulatedbyusingstandardDOMfunctions.
Note:
Aproperlyparseddocumentwilldisplaytothescreenas
>
xDoc=xmlread(...)
xDoc=
[#document:
null]
Example1:
AllXMLfileshaveasinglerootelement.SomeXMLfilesdeclareapreferredschemafileasanattributeofthiselement.
xDoc=xmlread(fullfile(matlabroot,'
toolbox/matlab/general/info.xml'
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- matlab xml 工具箱 docx
![提示](https://static.bdocx.com/images/bang_tan.gif)