이 번역 페이지는 최신 내용을 담고 있지 않습니다. 최신 내용을 영문으로 보려면 여기를 클릭하십시오.
xmlread
XML 문서 읽기 및 문서 객체 모델 노드 반환
설명
DOMnode = xmlread(
은 지정한 XML 파일을 읽어 들이고 문서를 표현하는 문서 객체 모델 노드 filename
)DOMnode
를 반환합니다.
xmlread
를 사용하려면 JAXP(Java® API for XML Processing)를 사용해야 합니다. 자세한 내용은 https://docs.oracle.com/javase/7/docs/api를 참조하십시오.
예제
XML 파일을 DOM(문서 객체 모델) 노드로 읽어 들이기
샘플 XML 파일의 내용을 검토한 다음 XML 파일을 DOM(문서 객체 모델) 노드로 읽어 들입니다.
sample.xml
파일의 내용을 표시합니다.
sampleXMLfile = 'sample.xml';
type(sampleXMLfile)
<productinfo> <matlabrelease>R2012a</matlabrelease> <name>Example Manager</name> <type>internal</type> <icon>ApplicationIcon.DEMOS</icon> <list> <listitem> <label>Example Manager</label> <callback>com.mathworks.xwidgets.ExampleManager.showViewer </callback> <icon>ApplicationIcon.DEMOS</icon> </listitem> </list> </productinfo>
XML 파일을 DOM 노드로 읽어 들입니다.
DOMnode = xmlread(sampleXMLfile);
XML 파일을 MATLAB® 구조체형 배열로 읽어 들이기
XML 파일을 MATLAB® 구조체로 읽어 들이는 구문 분석 함수를 만든 다음 샘플 XML 파일을 MATLAB 작업 공간으로 읽어 들입니다.
함수 parseXML
을 만들려면 이 코드를 복사하여 m 파일 parseXML.m
에 붙여 넣거나 이 예제에 포함된 parseXML.m
을 사용하십시오. parseXML
함수는 XML 파일의 데이터를 Name
, Attributes
, Data
및 Children
필드를 포함하는 MATLAB 구조체형 배열로 구문 분석합니다.
type('parseXML.m')
function theStruct = parseXML(filename) % PARSEXML Convert XML file to a MATLAB structure. try tree = xmlread(filename); catch error('Failed to read XML file %s.',filename); end % Recurse over child nodes. This could run into problems % with very deeply nested trees. try theStruct = parseChildNodes(tree); catch error('Unable to parse XML file %s.',filename); end % ----- Local function PARSECHILDNODES ----- function children = parseChildNodes(theNode) % Recurse over node children. children = []; if theNode.hasChildNodes childNodes = theNode.getChildNodes; numChildNodes = childNodes.getLength; allocCell = cell(1, numChildNodes); children = struct( ... 'Name', allocCell, 'Attributes', allocCell, ... 'Data', allocCell, 'Children', allocCell); for count = 1:numChildNodes theChild = childNodes.item(count-1); children(count) = makeStructFromNode(theChild); end end % ----- Local function MAKESTRUCTFROMNODE ----- function nodeStruct = makeStructFromNode(theNode) % Create structure of node info. nodeStruct = struct( ... 'Name', char(theNode.getNodeName), ... 'Attributes', parseAttributes(theNode), ... 'Data', '', ... 'Children', parseChildNodes(theNode)); if any(strcmp(methods(theNode), 'getData')) nodeStruct.Data = char(theNode.getData); else nodeStruct.Data = ''; end % ----- Local function PARSEATTRIBUTES ----- function attributes = parseAttributes(theNode) % Create attributes structure. attributes = []; if theNode.hasAttributes theAttributes = theNode.getAttributes; numAttributes = theAttributes.getLength; allocCell = cell(1, numAttributes); attributes = struct('Name', allocCell, 'Value', ... allocCell); for count = 1:numAttributes attrib = theAttributes.item(count-1); attributes(count).Name = char(attrib.getName); attributes(count).Value = char(attrib.getValue); end end
parseXML
함수를 사용하여 샘플 파일 info.xml
을 MATLAB 구조체로 구문 분석합니다.
sampleXMLfile = 'info.xml';
mlStruct = parseXML(sampleXMLfile)
mlStruct = struct with fields:
Name: 'productinfo'
Attributes: [1x2 struct]
Data: ''
Children: [1x13 struct]
입력 인수
filename
— 파일 이름
문자형 벡터 | string형 스칼라
파일 이름으로, 로컬 파일의 이름 또는 URL을 포함하는 문자형 벡터 또는 string형 스칼라로 지정됩니다.
데이터형: char
| string
버전 내역
MATLAB 명령
다음 MATLAB 명령에 해당하는 링크를 클릭했습니다.
명령을 실행하려면 MATLAB 명령 창에 입력하십시오. 웹 브라우저는 MATLAB 명령을 지원하지 않습니다.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)