Matlab中读取XML小记

按Matlab参考手册写的,直接上代码了:

function testXML(infilename) %简单的读写XML文件 try xDoc = xmlread(infilename); catch error('Failed to read XML file %s.',infilename); end allSchemeListItems = xDoc.getElementsByTagName('Scheme'); for i = 0 : allSchemeListItems.getLength-1 thisItem = allSchemeListItems.item(i); schemeName = char(thisItem.getAttributes.item(0).getValue); disp(['方案名:' schemeName]); childNode = thisItem.getFirstChild; while ~isempty(childNode) %读出此节点下所有Elements if childNode.getNodeType == childNode.ELEMENT_NODE %假设每个Element下都不再有子节点 tagName = char(childNode.getTagName); childText = char(childNode.getFirstChild.getData); disp([' ' tagName '= ' childText]); end % End IF childNode = childNode.getNextSibling; end % End WHILE end

 

XML文件:
<?xml version="1.0" encoding="UTF-8" ?> <!--IP Schemes - by IPSwitcher--> <document> <Scheme Name="宿舍"> <DHCP>Disabled </DHCP> <IP>219.230.81.160 </IP> <Mask>255.255.255.0 </Mask> <Gateway>219.230.81.1 </Gateway> <PrimaryDNS>218.2.135.1 </PrimaryDNS> <SpareDNS>0.0.0.0 </SpareDNS> </Scheme> <Scheme Name="实验室_dial"> <DHCP>Disabled </DHCP> <IP>219.230.98.13 </IP> <Mask>255.255.255.128 </Mask> <Gateway>219.230.98.1 </Gateway> <PrimaryDNS>218.2.135.1 </PrimaryDNS> <SpareDNS>202.102.24.35 </SpareDNS> </Scheme> <Scheme Name="实验室_Proxy"> <DHCP>Disabled </DHCP> <IP>219.230.98.61 </IP> <Mask>255.255.255.128 </Mask> <Gateway>219.230.98.1 </Gateway> <PrimaryDNS>218.2.135.1 </PrimaryDNS> <SpareDNS>202.102.24.35 </SpareDNS> </Scheme> </document>

 

调用示例:testXML('scheme.xml')

你可能感兴趣的:(xml,function,Scheme,File,matlab,encoding)