{"id":396,"date":"2010-09-13T12:32:28","date_gmt":"2010-09-13T12:32:28","guid":{"rendered":"https:\/\/blogs.mathworks.com\/desktop\/2010\/09\/13\/simple-xml-node-creation\/"},"modified":"2010-09-13T12:32:28","modified_gmt":"2010-09-13T12:32:28","slug":"simple-xml-node-creation","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2010\/09\/13\/simple-xml-node-creation\/","title":{"rendered":"Simple XML Node Creation"},"content":{"rendered":"<p>Last time in my <a href=\"https:\/\/blogs.mathworks.com\/community\/2010\/06\/28\/using-xml-in-matlab\/\">XML series<\/a>, I showed you how to use <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/xmlread.html\"><tt>xmlread<\/tt><\/a> to create an XML Document object in MATLAB. I also promised to follow up on that with more information on how to use the Document object. Today, I want to show you how to create a DOM from scratch and then build up a simple XML document. To do that, I'm going use the canonical address book example as our sample XML document.<\/p>\n<p>The first step is to create a new node. To do this, we need to use the Java method <tt>com.mathworks.xml.XMLUtils.createDocument()<\/tt> since MATLAB does not have native XML objects. In this example we create the top level (\"root\" or \"document\") node to be named <tt>AddressBook<\/tt>. Using the one argument version of <tt>xmlwrite<\/tt> will display the xml document in the Command Window. <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">docNode = com.mathworks.xml.XMLUtils.createDocument(<span style=\"color: #A020F0\">'AddressBook'<\/span>);\r\nxmlwrite(docNode)<\/pre>\n<pre style=\"font-style:oblique\">\r\nans =\r\n\r\n&lt;?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n&lt;AddressBook\/>\r\n\r\n<\/pre>\n<p>Now that I have the document element, I want to populate it with child nodes. The document object is also the factory for new nodes. Therefore each time I create a new node, I do so from my <tt>docNode<\/tt> object. Also note, that I can't append a child element node to the document directly, I have to first call <tt>geDocumentElement<\/tt> to get the root element node of the document. It's all a little confusing, but I'm sure it made sense to the original designers of Apache's Xerces DOM (which is the implementation we use).<\/p>\n<p>In the next step, I create an <tt>Entry<\/tt> element to represent a single person in my address book, and append it as a child to the root node.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">entry_node = docNode.createElement(<span style=\"color: #A020F0\">'Entry'<\/span>);\r\ndocNode.getDocumentElement.appendChild(entry_node);\r\nxmlwrite(docNode)<\/pre>\n<pre style=\"font-style:oblique\">\r\nans =\r\n\r\n&lt;?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n&lt;AddressBook>\r\n   &lt;Entry\/>\r\n&lt;\/AddressBook>\r\n\r\n<\/pre>\n<p>Now that there is an <tt>Entry<\/tt> in the address book, I will create data elements for it. To do this I create new element nodes and append those to my <tt>Entry<\/tt> node. I also create text Nodes to represent the text data of each of these elements. For example, a I create a \"<tt>Name<\/tt>\" node for the person's name, and put \"Friendly Mathworker\" (a colleague) as text node child of the <tt>Name<\/tt> element. Doing this allows me to build up the XML tree structure. In addition to text nodes, we can create attribute nodes (see next section), as well as CDATA, Comments, and other types of XML nodes. <\/p>\n<p>add name, phone number<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">name_node = docNode.createElement(<span style=\"color: #A020F0\">'Name'<\/span>);\r\nname_text = docNode.createTextNode(<span style=\"color: #A020F0\">'Friendly J. Mathworker'<\/span>);\r\nname_node.appendChild(name_text);\r\nentry_node.appendChild(name_node);\r\n\r\nphone_number_node = docNode.createElement(<span style=\"color: #A020F0\">'PhoneNumber'<\/span>);\r\nphone_number_text = docNode.createTextNode(<span style=\"color: #A020F0\">'(508) 647-7000'<\/span>);\r\nphone_number_node.appendChild(phone_number_text);\r\nentry_node.appendChild(phone_number_node);\r\n\r\nxmlwrite(docNode)<\/pre>\n<pre style=\"font-style:oblique\">\r\nans =\r\n\r\n&lt;?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n&lt;AddressBook>\r\n   &lt;Entry>\r\n      &lt;Name>Friendly J. Mathworker&lt;\/Name>\r\n      &lt;PhoneNumber>(508) 647-7000&lt;\/PhoneNumber>\r\n   &lt;\/Entry>\r\n&lt;\/AddressBook>\r\n\r\n<\/pre>\n<p>For the final step, I'm going to add an address to this person's entry. In addition to a simple a text node, I've added some attributes using two different methods. First, I use the convenience <tt>setAttribute(name, value)<\/tt> method to indicate that this <tt>Address<\/tt> is of <tt>type<\/tt> \"<tt>work<\/tt>\". In the second case I use the more formal node structure to create a \"<tt>hasZip<\/tt>\" attribute to indicate that I left off my company's zip code from the address. Note that this implementation of <tt>xmlwrite<\/tt> alphabetically sorts the attributes when displaying the document, whereas element nodes stay in the order in which they are appended. <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">address_node = docNode.createElement(<span style=\"color: #A020F0\">'Address'<\/span>);\r\naddress_node.setTextContent(<span style=\"color: #A020F0\">'3 Apple Hill Dr, Natick MA'<\/span>)\r\n<span style=\"color: #228B22\">% set an attribute directly<\/span>\r\naddress_node.setAttribute(<span style=\"color: #A020F0\">'type'<\/span>,<span style=\"color: #A020F0\">'work'<\/span>);\r\n\r\nentry_node.appendChild(address_node);\r\n\r\n<span style=\"color: #228B22\">% or create the attribute as a node<\/span>\r\nhas_zip_attribute = docNode.createAttribute(<span style=\"color: #A020F0\">'hasZip'<\/span>);\r\nhas_zip_attribute.setNodeValue(<span style=\"color: #A020F0\">'no'<\/span>);\r\naddress_node.setAttributeNode(has_zip_attribute);\r\n\r\nxmlwrite(docNode)<\/pre>\n<pre style=\"font-style:oblique\">\r\nans =\r\n\r\n&lt;?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n&lt;AddressBook>\r\n   &lt;Entry>\r\n      &lt;Name>Friendly J. Mathworker&lt;\/Name>\r\n      &lt;PhoneNumber>(508) 647-7000&lt;\/PhoneNumber>\r\n      &lt;Address hasZip=\"no\" type=\"work\">3 Apple Hill Dr, Natick MA&lt;\/Address>\r\n   &lt;\/Entry>\r\n&lt;\/AddressBook>\r\n\r\n<\/pre>\n<p>Next time I post about XML, I'll describe how to navigate a DOM to find data of interest. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last time in my XML series, I showed you how to use xmlread to create an XML Document object in MATLAB. I also promised to follow up on that with more information on how to use the Document object.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2010\/09\/13\/simple-xml-node-creation\/\">read more >><\/a><\/p>\n","protected":false},"author":38,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[31],"tags":[56],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/396"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=396"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/396\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}