Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Check if and add element to XML file
Message
 
 
To
03/12/2018 19:58:00
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01659959
Message ID:
01664030
Views:
41
>>
>>How would you change the above code if you want to check the element as a sub-element. Here is the modified version of the XML file:
>>
>>TEXT TO m.Source NOSHOW
>><?xml version="1.0" standalone="yes" ?>
>><Setting>
>>        <SubSetting>
>>	             <MyElement1>123</MyElement1>
>>        </SubSetting>
>></Setting>
>>ENDTEXT
>>
>>
>>As you can see, above, I added the SubSetting and want to check and add an element MyElement2 there. I did try to change the code myself but keep getting an error.
>>
>>TIA
>
>Dmitry,
>
>A revision:
>
>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)
>
>
>I believe this works even if you need to add more levels to the tree of the required path, or if the tree is already partially built.

Antonio,
Thank you very much!
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform