フィルターのクリア

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

34 ビュー (過去 30 日間)
Mateusz Malinowski
Mateusz Malinowski 2016 年 3 月 18 日
回答済み: ANKAN BHATTACHARYYA 2016 年 11 月 20 日
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 Center および File ExchangeStructured 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