Main Content

xmlread

XML 문서 읽기 및 문서 객체 모델 노드 반환

설명

예제

DOMnode = xmlread(filename)은 지정한 XML 파일을 읽어 들이고 XML 파일의 구문 분석된 버전을 나타내는 Apache® Xerces-J 문서 객체를 반환합니다. Apache Xerces-J는 JAXP(Java® API for XML Processing)를 구현합니다. JAXP 함수를 사용하여 이 문서 객체를 조작합니다. Apache Xerces-J에 대한 자세한 내용은 https://xerces.apache.org/xerces-j/apiDocs/를 참조하십시오.

예제

DOMnode = xmlread(filename,'AllowDoctype',tf)는 DOCTYPE 선언이 허용되는지 여부도 지정합니다. tffalse이면 DOCTYPE 선언을 포함하는 입력 XML 파일을 읽을 경우 오류가 발생합니다. 그렇지 않으면 xmlread는 XML 파일에 대해 출력값 DOMnode를 반환합니다. tf의 디폴트 값은 true입니다.

예제

모두 축소

샘플 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 작업 공간으로 읽어 들입니다.

함수 parseXML을 만들려면 이 코드를 복사하여 m 파일 parseXML.m에 붙여 넣으십시오. parseXML 함수는 XML 파일의 데이터를 Name, Attributes, DataChildren 필드를 포함하는 MATLAB 구조체형 배열로 구문 분석합니다.

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]

입력 인수

모두 축소

파일 이름으로, 로컬 파일의 이름 또는 URL을 포함하는 문자형 벡터 또는 string형 스칼라로 지정됩니다.

데이터형: char | string

버전 내역

R2006a 이전에 개발됨