Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Check if and add element to XML file
Message
 
 
À
13/05/2018 15:53:09
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
01659959
Message ID:
01659968
Vues:
54
>>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)
>
>* you can save the complete file, just uncomment the next line
>* m.XML.Save(GETFILE("xml"))
>
Hi Antonio,

Thank you very much for the code. I am concerned though (and maybe simply because I have never used it) that "MSXML2.DOMDocument.6.0" has to be installed on the user PC. Honestly I don't know what it is and if it is part of any Windows OS or has to be installed as add-on.
"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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform