>>TEXT TO m.Source NOSHOW >><?xml version="1.0" standalone="yes" ?> >><Setting> >> <SubSetting> >> <MyElement1>123</MyElement1> >> </SubSetting> >></Setting> >>ENDTEXT >>>>
>LOCAL Source AS String > >* your source document (the contents of your "MyFile.xml") >TEXT TO m.Source NOSHOW ><?xml version="1.0" standalone="yes" ?> ><Setting> > <MyElement1>123</MyElement1> ></Setting> >ENDTEXT > >* what must exist in the document, and the initial value >LOCAL RequiredPath AS String >LOCAL DefaultContent AS String > >m.RequiredPath = "/Setting/SubSetting/MyElement2" >m.DefaultContent = "dummy" > >* our objects to access the DOM >LOCAL XML AS MSXML2.DOMDocument60 >LOCAL Ancestor AS MSXML2.IXMLDOMNode >LOCAL NewElement AS MSXML2.IXMLDOMElement >LOCAL TextElement AS MSXML2.IXMLDOMText > >* to construct the path to the element >LOCAL InPath AS String >LOCAL DepthLevel AS Integer > >m.XML = CREATEOBJECT("MSXML2.DOMDocument.6.0") >m.XML.Async = .F. >m.XML.LoadXML(m.Source) > >* if the required path does not exist... >IF m.XML.Selectnodes(m.RequiredPath).length = 0 > * get what will be its ancestor > m.Ancestor = m.XML.Selectnodes(LEFT(m.RequiredPath, RAT("/", m.RequiredPath) - 1)).item(0) > * make sure all the precedent tree is available > IF ISNULL(m.Ancestor) > * we can start at the first level below the root node > * so we may skip the first and second slashes > FOR m.DepthLevel = 3 TO OCCURS("/", m.RequiredPath) > * get the interim path > m.InPath = LEFT(m.RequiredPath, AT("/", m.RequiredPath, m.DepthLevel) - 1) > m.Ancestor = m.XML.Selectnodes(m.InPath).item(0) > * if does not exist > IF ISNULL(m.Ancestor) > * create the interim level > m.Ancestor = m.XML.Selectnodes(LEFT(m.InPath, RAT("/", m.InPath) - 1)).item(0) > m.NewElement = m.XML.Createelement(SUBSTR(m.InPath, RAT("/", m.InPath) + 1)) > m.Ancestor.appendChild(m.NewElement) > * this will be the new ancestor (in case we reached the end of the levels before the required path) > m.Ancestor = m.NewElement > ENDIF > ENDFOR > ENDIF > * create a new element > m.NewElement = m.XML.CreateElement(SUBSTR(m.RequiredPath, RAT("/", m.RequiredPath) + 1)) > * set its value > m.NewElement.text = m.DefaultContent > * and append it to the end of its parent > m.Ancestor.appendChild(m.NewElement) > * create a text element with a line feed > m.TextElement = m.XML.Createtextnode(CHR(10)) > * and append to the ancestor, also > m.Ancestor.appendChild(m.TextElement) >ENDIF > >* the DOM is reconstructed... >MESSAGEBOX(m.XML.XML) >>