필터 지우기
필터 지우기

How do I append a node to an '.xml' structure within a function?

조회 수: 14 (최근 30일)
Paul Sponagle
Paul Sponagle 2016년 8월 2일
답변: Paul Sponagle 2016년 8월 8일
I am trying to send an xml structure to a function, append a new node, and return the modified structure. This is because the child structure being appended will be very common to many '.xml' files and I don't want to re-write the same code every time.
If I am not in a function the following works:
docNode = com.mathworks.xml.XMLUtils.createDocument('ugcs-Transfer');
parent_node = docNode.createElement('parent')
docNode.appendChild(parent_node)
child_node = docNode.createElement('child');
parent_node.appendChild(child_node);
If I try to pass it to a function like this:
docNode = com.mathworks.xml.XMLUtils.createDocument('ugcs-Transfer');
parent_node = docNode.createElement('parent')
docNode.appendChild(parent_node)
docNode = myFunction(docNode)
This function will not append the child to the parent node:
Z = my_function(docNode)
child_node = docNode.createElement('child');
parent_node.appendChild(child_node); % This line produces an error: Undefined variable "parent_node" or ...
% class "parent_node.appendChild".
Z = docNode
end
The desired end state would be:
%<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
% <parent>
% <child>
Any help would be appreciated,
Paul

채택된 답변

Paul Sponagle
Paul Sponagle 2016년 8월 8일
I got the answer for this from a colleague.
The line of code I needed was:
parent_node=docNode1.getElementsByTagName('Parent').item(0)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structured Data and XML Documents에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by