문서 객체 모델을 XML 파일로 내보내기
matlab.io.xml.dom.DOMWriter
객체 또는 xmlwrite
함수를 사용하여 DOM(문서 객체 모델) 문서 노드를 XML 파일로 내보낼 수 있습니다.
matlab.io.xml.dom.DOMWriter
클래스는 MAXP(MATLAB® API for XML Processing)에 속합니다. MAXP DOMWriter
객체를 사용하려면 DOM 문서 노드를 matlab.io.xml.dom.Document
객체로 표현하십시오. 요소, 텍스트 및 그 밖의 노드를 만들어 문서 노드에 추가하려면 MAXP 클래스와 메서드를 사용하십시오. matlab.io.xml.dom
을 참조하십시오. MAXP 클래스를 사용할 때 Java® 소프트웨어는 필요하지 않습니다.
xmlwrite
를 사용하여 작성할 수 있는 DOM 문서를 만들려면 com.mathworks.xml.XMLUtils.createDocument
를 사용하십시오. 노드를 만들어 문서 노드에 추가하려면 JAXP(Java API for XML Processing)의 메서드를 사용하십시오. https://docs.oracle.com/javase/7/docs/api
에서 org.w3c.dom
패키지 설명을 참조하십시오.
DOM 문서 만들기
다음은 XML 문서를 생성하기 위한 일반적인 단계입니다.
문서 노드를 만들고 루트 요소를 정의합니다. 다음 코드는 MAXP
matlab.io.xml.dom.Document
객체를 생성하여 문서 노드를 만듭니다.import matlab.io.xml.dom.* docNode = Document('root_element');
다음 코드는 JAXP 메서드와 함께 사용할 수 있는 문서 노드를 만듭니다.
docNode = com.mathworks.xml.XMLUtils.createDocument('root_element');
getDocumentElement
를 호출하여 루트 요소에 해당하는 노드를 가져옵니다. 루트 요소 노드는 자식 노드를 추가하기 위해 필요합니다.문서 노드의 메서드를 호출하여 요소 노드, 텍스트 노드, 주석 노드 및 특성 노드를 추가합니다. 유용한 메서드는 다음과 같습니다.
createElement
createTextNode
createComment
setAttribute
appendChild
를 사용하여 자식 노드를 부모 노드에 추가합니다.팁
텍스트 노드는 항상 요소 노드의 자식 노드입니다. 텍스트 노드를 추가하려면, 문서 노드에
createTextNode
를 사용한 다음 부모 요소 노드에appendChild
를 사용하십시오.
MAXP DOMWriter
객체를 사용하여 XML 파일에 DOM 문서 노드 쓰기
이 예제에서는 matlab.io.xml.dom.DOMWriter
객체를 사용하여 Upslope Area Toolbox에 대한 info.xml
파일을 생성합니다(사용자 지정 문서 표시하기의 설명 참조).
문서 노드와 루트 요소 toc
를 생성합니다.
import matlab.io.xml.dom.* docNode = Document('toc');
루트 요소를 가져와서 version
특성을 설정합니다.
toc = docNode.getDocumentElement; setAttribute(toc,'version','2.0');
제품 페이지에 대한 tocitem
요소 노드를 추가합니다. 이 파일의 각 tocitem
요소는 target
특성과 자식 텍스트 노드를 갖습니다.
product = createElement(docNode,'tocitem'); setAttribute(product,'target','upslope_product_page.html'); appendChild(product,createTextNode(docNode,'Upslope Area Toolbox')); appendChild(toc,product);
주석을 추가합니다.
appendChild(product,createComment(docNode,' Functions '));
각 함수에 대해 tocitem
요소 노드를 추가합니다.
functions = {'demFlow','facetFlow','flowMatrix','pixelFlow'}; n = numel(functions); for idx = 1:n curr_node = createElement(docNode,'tocitem'); curr_file = [functions{idx} '_help.html']; setAttribute(curr_node,'target',curr_file); % Child text is the function name. appendChild(curr_node,createTextNode(docNode,functions{idx})); appendChild(product,curr_node); end
DOM 노드를 info.xml
로 내보낸 다음 파일을 확인합니다.
xmlFileName = 'info.xml'; writer = matlab.io.xml.dom.DOMWriter; writer.Configuration.FormatPrettyPrint = true; writeToFile(writer,docNode,xmlFileName); type('info.xml');
<?xml version="1.0" encoding="UTF-8" standalone="no" ?> <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>
xmlwrite
를 사용하여 XML 파일에 DOM 문서 노드 쓰기
이 예제에서는 xmlwrite
를 사용하여 Upslope Area Toolbox에 대한 info.xml
파일을 생성합니다(사용자 지정 문서 표시하기의 설명 참조).
docNode = com.mathworks.xml.XMLUtils.createDocument('toc'); toc = docNode.getDocumentElement; toc.setAttribute('version','2.0'); 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 ')); 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 xmlwrite('info.xml',docNode); type('info.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>
기존 XML 파일 업데이트하기
기존 파일의 데이터를 변경하려면 다음과 같이 하십시오.
matlab.io.xml.dom.Parser
객체 또는xmlread
를 사용하여 파일을 DOM 문서 노드로 가져옵니다.노드를 순회하며 다음과 같은 메서드를 사용하여 데이터를 추가하거나 변경합니다.
getElementsByTagName
getFirstChild
getNextSibling
getNodeName
getNodeType
matlab.io.xml.dom.Parser
를 사용하여 XML 파일을matlab.io.xml.dom.Document
로 읽어 들이는 경우 MAXP(MATLAB API for XML Processing) 클래스와 메서드를 사용하십시오.matlab.io.xml.dom
을 참조하십시오.xmlread
를 사용하는 경우에는 JAXP(Java API for XML Processing) 메서드를 사용해야 합니다.https://docs.oracle.com/javase/7/docs/api
에서org.w3c.dom
패키지 설명을 참조하십시오.DOM 문서에 모든 변경 내용이 포함되면 파일을 씁니다. MAXP DOM 문서에는
matlab.io.xml.DOMWriter
객체를 사용하십시오. JAXP DOM 문서에는xmlwrite
를 사용하십시오.
참고 항목
matlab.io.xml.dom.Document
| matlab.io.xml.dom.DOMWriter
| xmlwrite