Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Check if and add element to XML file
Message
 
 
To
18/05/2018 14:31:15
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01659959
Message ID:
01660119
Views:
40
>>>>Hi,
>>>>
>>>>I am looking for an efficient way to check if an XML has an element, and if the element does not exist, add it.
>>>>
>>>>Here is an example:
>>>>MyFile.xml:
>>>>
>>>><?xml version="1.0" standalone="yes" ?>
>>>><Setting>
>>>>   <MyElement1>123</MyElement1>
>>>></Setting>
>>>>
>>>>
>>>>Say, I want to find if this file (e.g. MyFile.xml) has element "MyElement2". And if not, add it.
>>>>I can do it with FILETOSTRING(), STRTOFILE(), and STREXTRACT() functions. But I am not sure if this is the most efficient approach.
>>>>
>>>>Any suggestions? TIA
>>>
>>>Dmitry
>>>
>>>You may use the DOM to perform this operation, instead of parsing the XML file in VFP (things may start to be really nasty, if you go that way...). Your requirements are fairly simple - for now! - so no need to create an elaborated transformation.
>>>
>>>
>>>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/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
>>>
>>>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)
>>>	* 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)
>>>ENDIF
>>>
>>>* the DOM is reconstructed...
>>>MESSAGEBOX(m.XML.XML)
>>>
>>>
>>
>>Hi Antonio,
>>
>>Sorry for a newbie question. I don't understand something. When the resulting XML file is shown (your last messagebox()), the new element is indented, just like the existing element. Where in your code do you specify (or how is it done) that the new element is indented into the string exactly as the previous element?
>
>That's a very good question, Dmitry. It has to do on how the DOM handles space in whitespace only text nodes (fragments of the document that consist only in spaces, tabs, carriage returns and line feeds around elements). If you add an xml:space="preserve" to the Setting element, the DOM will not add space for indentation purposes, and start the new element right at the bottom of the ancestor, that is, exactly at the position before </Setting>.
>
>Have a try:
>
>
>TEXT TO m.Source NOSHOW
><?xml version="1.0" standalone="yes" ?>
><Setting xml:space="preserve">
>   <MyElement1>123</MyElement1>
></Setting>
>ENDTEXT
>
Thank you very much for the explanation, Antonio.
"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
Reply
Map
View

Click here to load this message in the networking platform