Main Content

xmlwrite

XML 문서 객체 모델 노드 쓰기

설명

예제

xmlwrite(filename,DOMnode)는 문서 객체 모델(DOM) 노드 DOMnodefilename 파일에 씁니다.

xmlwrite를 사용하려면 JAXP(Java® API for XML Processing)를 사용해야 합니다. 자세한 내용은 https://docs.oracle.com/javase/7/docs/api를 참조하십시오.

예제

chr = xmlwrite(DOMnode)는 직렬화된 DOM 노드를 문자형 벡터로 반환합니다.

예제

모두 축소

우선 XML 데이터를 포함하는 DOM(문서 객체 모델) 노드를 만드는 것으로 XML 파일 쓰기를 시작하십시오. 그런 다음 DOM 노드를 XML 파일에 씁니다. 최종 XML 파일은 다음 텍스트를 포함해야 합니다.

<?xml version="1.0" encoding="utf-8"?>

<toc version="2.0">

<tocitem target="upslope_product_page.html">Upslope Area Toolbox<!-- Functions -->

<tocitem target="demFlow_help.html">demFlow</tocitem>

<tocitem target="facetFlow_help.html">facetFlow</tocitem>

<tocitem target="flowMatrix_help.html">flowMatrix</tocitem>

<tocitem target="pixelFlow_help.html">pixelFlow</tocitem>

</tocitem>

</toc>

먼저 DOM 노드 객체와 루트 요소를 만든 다음 XML 데이터에 대응되는 노드의 요소와 특성을 채웁니다.

docNode = com.mathworks.xml.XMLUtils.createDocument('toc');

루트 요소를 식별하고 version 특성을 설정합니다.

toc = docNode.getDocumentElement;
toc.setAttribute('version','2.0');

제품 페이지에 대한 tocitem 요소 노드를 추가합니다. 이 파일의 각 tocitem 요소는 target 특성과 자식 텍스트 노드를 갖습니다.

product = docNode.createElement('tocitem');
product.setAttribute('target','upslope_product_page.html');
product.appendChild(docNode.createTextNode('Upslope Area Toolbox'));
toc.appendChild(product);

주석을 추가합니다.

product.appendChild(docNode.createComment(' Functions '));

각 함수에 대해 tocitem 요소 노드를 추가합니다. 여기서 target의 형식은 function_help.html입니다.

functions = {'demFlow','facetFlow','flowMatrix','pixelFlow'};
for idx = 1:numel(functions)
    curr_node = docNode.createElement('tocitem');
    
    curr_file = [functions{idx} '_help.html']; 
    curr_node.setAttribute('target',curr_file);
    
    % Child text is the function name.
    curr_node.appendChild(docNode.createTextNode(functions{idx}));
    product.appendChild(curr_node);
end

마지막으로, DOM 노드를 XML 파일 infoUAT.xml로 내보내고, type 함수를 사용하여 파일을 확인합니다.

xmlwrite('infoUAT.xml',docNode);
type('infoUAT.xml');

샘플 XML 파일에서 DOM 노드를 읽어 들이고 DOM 노드의 내용을 문자형 벡터로 가져옵니다.

샘플 XML 파일의 내용을 표시하고, 파일에서 DOM 노드를 가져옵니다.

sampleXMLfile = 'sample.xml';
type(sampleXMLfile)
DOMnode = xmlread(sampleXMLfile);

xmlwrite를 사용하여 DOMnode 객체를 직렬화된 문자형 벡터로 반환합니다.

text = xmlwrite(DOMnode)
text = 
    '<?xml version="1.0" encoding="utf-8"?>
     <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>'

입력 인수

모두 축소

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

데이터형: char | string

DOM(문서 객체 모델) 노드로, DOM 노드 객체로 지정됩니다.

문서 객체 모델(DOM)은 World Wide Web Consortium에서 정의합니다. 자세한 내용은 XML 문서 객체 모델 항목을 참조하십시오.

버전 내역

R2006a 이전에 개발됨