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

10 次查看(过去 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