필터 지우기
필터 지우기

How can I add XML attributes and corresponding values in line to RootNode

조회 수: 34 (최근 30일)
Hello,
I am trying to create an XML document using the following code:
docNode = com.mathworks.xml.XMLUtils.createDocument...
('Part')
docRootNode = docNode.getDocumentElement;
docRootNode.setAttribute('Name','Piston');
ChildNode_Names={'PartType', 'PartInventorySerialNumber', 'PartCreateDate',...
'PartReport', 'PartActive'};
ChildNode_Values={'Ring', '23', '11/20/2015', '1', '1'};
for i=1:5
thisElement = docNode.createElement(ChildNode_Names(i));
thisElement.appendChild...
(docNode.createTextNode(sprintf('%s',ChildNode_Values{i})));
docRootNode.appendChild(thisElement);
end
xmlFileName = ['Part.xml'];
xmlwrite(xmlFileName,docNode);
type(xmlFileName);
This is the XML document which I create:
I am trying to add attributes and corresponding values directly under (in-line with) the text node elements.
I can't seem to be able to add an attribute unless I add it as a child to one of the text node elements, such as below:
Done using this code:
docNode = com.mathworks.xml.XMLUtils.createDocument...
('Part')
docRootNode = docNode.getDocumentElement;
docRootNode.setAttribute('Name','Piston');
ChildNode_Names={'PartType', 'PartInventorySerialNumber', 'PartCreateDate',...
'PartReport', 'PartActive'};
ChildNode_Values={'Ring', '23', '11/20/2015', '1', '1'};
for i=1:5
thisElement = docNode.createElement(ChildNode_Names(i));
thisElement.appendChild...
(docNode.createTextNode(sprintf('%s',ChildNode_Values{i})));
docRootNode.appendChild(thisElement);
end
entry_node = docNode.createElement('Entry');
docNode.getDocumentElement.appendChild(entry_node);
address_node = docNode.createElement('Address');
address_node.setTextContent('3 Apple Hill Dr, Natick MA')
% set an attribute directly
address_node.setAttribute('type','work');
entry_node.appendChild(address_node);
% or create the attribute as a node
has_zip_attribute = docNode.createAttribute('hasZip');
has_zip_attribute.setNodeValue('no');
address_node.setAttributeNode(has_zip_attribute);
xmlFileName = ['Part.xml'];
xmlwrite(xmlFileName,docNode);
type(xmlFileName);
This is not what I am looking for though.
I need my attributes and the corresponding values on the same line, and directly inline with the PartType node, like in the image below (sorry for having to cover up the variables):
Can someone please help me?

채택된 답변

Kirby Fears
Kirby Fears 2016년 3월 18일
편집: Kirby Fears 2016년 3월 18일
Hi Mateusz,
You can add attributes directly to element nodes as well. I am providing code to create the < Attribute/ > node with Name and Value attributes shown below.
Here's code to create the < Part > root node along with the < Attribute /> node:
docNode = com.mathworks.xml.XMLUtils.createDocument('Part');
docRootNode = docNode.getDocumentElement;
docRootNode.setAttribute('Name','Piston');
% Make an element node called 'Attribute'
attrNode = docNode.createElement('Attribute');
% assign Name and Value attributes
attrNode.setAttribute('Name','MyAttr');
attrNode.setAttribute('Value','1');
% append as a child of root node
docRootNode.appendChild(attrNode);
% display xml
xmlwrite(docRootNode)
ans =
<Part Name="Piston">
<Attribute Name="MyAttr" Value="1"/>
</Part>
You can use this example to design your XML document as desired.
Hope this helps.
  댓글 수: 1
Mateusz Malinowski
Mateusz Malinowski 2016년 3월 21일
편집: Mateusz Malinowski 2016년 3월 21일
Hi Kirby,
That did exactly what I needed it to do. My unfamiliarity with xml led me to forcefully trying to add an attribute to the child node there.
Thank you for your help.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

ANKAN BHATTACHARYYA
ANKAN BHATTACHARYYA 2016년 11월 20일

if i have <stroke stroke_no="2" / > then how can i do : <stroke stroke_no="2" stroke_name="XYZ" / > ?

카테고리

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