gcue Posted May 14, 2012 Posted May 14, 2012 (edited) hello world i am trying to remove <statusTexts>...</statusTexts> from an xml file i tried doing it this way but it seems to render the xml file useless (think it has to do with the spacing - xml files are space sensitive?) $data = FileRead($xml_file) $new_data= StringRegExpReplace($data, "<statusTexts>([V]*)</statusTexts>", "") FileOpen($xml_file, 2) FileWrite($xml_file, $new_data) i have also tried using _XMLDomWrapper but have not had success using _XMLDeleteNode (bc I dont think <statusTexts> is a node?) here's the XML file any help is greatly appreciated! <?xml version="1.0" encoding="UTF-8"?><imClientConfig buddylist-version="1.0" external-mod="" lastUpdated="1336984323374" version="1.0"><communities><community alwaysEditActiveText="true" alwaysEditAwayText="true" alwaysEditDndText="true" authServerUrl="" authType="TAM-SPNEGO" autoAwayTimeout="20" availWhenUnlocked="true" cloud="false" editable="" email="" iconIsUserSpecified="false" iconPath="" internetFilter="" isAutoActiveEnabled="true" isAutoAwayEnabled="true" isAutoAwayTimeoutEnabled="true" keepAliveInterval="60" loginAtStartup="true" loginByToken="false" managed="false" partnerIdType="" port="1533" primry="false" readNotesCanonicalSetting="false" resolveLivenames="false" savePassword="true" sendKeepAlive="true" useCustomTextForAutoAway="false" useGlobalConnSettings="true" useOsPass="false" useServerForLookup="true"><connection connectionType="direct" proxyHost="" proxyPort="" proxyResolvesLocally="false" proxyUserName="" tokenHostUrl="" transport=""/><statusTexts><activeText>I am available, how can I help you...?</activeText><activeText>I am available, supporting from home...</activeText><activeText>I'm available, supporting from home...</activeText><activeText>How can I help you today!</activeText><activeText>I am available</activeText><activeText>???</activeText><awayText>??????????????</awayText><awayText>I'm away for lunch time, will respond as soon as I get back...</awayText><awayText>Sorry...away from my computer now...</awayText><awayText>I am away from my computer now</awayText><inMtgText>Sorry, I am in a meeting! Can I get back to you later?</inMtgText><inMtgText>Sorry, in meeting now!</inMtgText><inMtgText>?????</inMtgText><inMtgText>I am in a meeting</inMtgText><dndText>Sorry, I am in a meeting! Can I get back to you later?</dndText><dndText>Sorry, in meeting now!</dndText><dndText>???????</dndText><dndText>Please do not disturb me</dndText><idleText>??????????????</idleText><idleText>I am away from my computer now</idleText><lockText>??????????????</lockText><lockText>I am away from my computer now</lockText></statusTexts></community></communities></imClientConfig> Edited May 14, 2012 by gcue
jdelaney Posted May 14, 2012 Posted May 14, 2012 http://msdn.microsoft.com/en-us/library/55aza6c2look into removeall method, or removechild method IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
gcue Posted May 14, 2012 Author Posted May 14, 2012 looks like its not working, because the node contains foreign language characters - if i process one that doesn't have special characters then it works fine.
jdelaney Posted May 14, 2012 Posted May 14, 2012 (edited) $oXML=ObjCreate("Microsoft.XMLDOM") $doc = $oXML.documentelement ;MsgBox ( 4096,"test",$doc.text ) $oXML.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" & _ "<title>Pride And Prejudice</title>" & _ "</book>") ConsoleWrite ( $oXML.xml & @CRLF) $oXMLChild = $oXML.selectSingleNode( "//title" ) ;$oXMLChild = $oXML.selectSingleNode( "//items/item[@name=""racecar""]/type[0]" ) ConsoleWrite ( $oXMLChild.xml & @CRLF) $root = $oXML.documentelement $test = $root.removeChild ($oXMLChild ) ConsoleWrite ( $oXML.xml & @CRLF) This removes the 'title' node...and really? provide the xml structure, snippet, plz. Edited May 14, 2012 by jdelaney IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
gcue Posted May 14, 2012 Author Posted May 14, 2012 <?xml version="1.0" encoding="UTF-8"?><imClientConfig buddylist-version="1.0" external-mod="" lastUpdated="1336984323374" version="1.0"><communities><community alwaysEditActiveText="true" alwaysEditAwayText="true" alwaysEditDndText="true" authServerUrl="" authType="TAM-SPNEGO" autoAwayTimeout="20" availWhenUnlocked="true" cloud="false" editable="" email="" iconIsUserSpecified="false" iconPath="" internetFilter="" isAutoActiveEnabled="true" isAutoAwayEnabled="true" isAutoAwayTimeoutEnabled="true" keepAliveInterval="60" loginAtStartup="true" loginByToken="false" managed="false" partnerIdType="" port="1533" primry="false" readNotesCanonicalSetting="false" resolveLivenames="false" savePassword="true" sendKeepAlive="true" useCustomTextForAutoAway="false" useGlobalConnSettings="true" useOsPass="false" useServerForLookup="true"><connection connectionType="direct" proxyHost="" proxyPort="" proxyResolvesLocally="false" proxyUserName="" tokenHostUrl="" transport=""/><statusTexts><activeText>I am available, how can I help you...?</activeText><activeText>I am available, supporting from home...</activeText><activeText>I'm available, supporting from home...</activeText><activeText>How can I help you today!</activeText><activeText>I am available</activeText><activeText>応答忯</activeText><awayText>コンピュータを使㿣㿦㿄㿾㿛ん</awayText><awayText>I'm away for lunch time, will respond as soon as I get back...</awayText><awayText>Sorry...away from my computer now...</awayText><awayText>I am away from my computer now</awayText><inMtgText>Sorry, I am in a meeting! Can I get back to you later?</inMtgText><inMtgText>Sorry, in meeting now!</inMtgText><inMtgText>会議中㿧㿙</inMtgText><inMtgText>I am in a meeting</inMtgText><dndText>Sorry, I am in a meeting! Can I get back to you later?</dndText><dndText>Sorry, in meeting now!</dndText><dndText>応答㿧㿿㿾㿛ん</dndText><dndText>Please do not disturb me</dndText><idleText>コンピュータを使㿣㿦㿄㿾㿛ん</idleText><idleText>I am away from my computer now</idleText><lockText>コンピュータを使㿣㿦㿄㿾㿛ん</lockText><lockText>I am away from my computer now</lockText></statusTexts></community></communities></imClientConfig>
jdelaney Posted May 14, 2012 Posted May 14, 2012 that XML seems to be invalid...you close off a node, but then also have a closing node...<community ....lots of attributes... /> lots of elements</community> double check the full structure. IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
jdelaney Posted May 14, 2012 Posted May 14, 2012 It's odd, it almost seems like you can't delete something that's nested beyond one layer... IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
gcue Posted May 14, 2012 Author Posted May 14, 2012 (edited) it lets me delete it when theres no special characters.. using this: _XMLFileOpen($xml_file) _XMLDeleteNode("//imClientConfig/communities/*") if @error then MsgBox(0,@error,"cant find") Edited May 14, 2012 by gcue
gcue Posted May 14, 2012 Author Posted May 14, 2012 actually found my mistake.. stringregexpreplace works just fine =) $data = FileRead($xml_file) $new_data= StringRegExpReplace($data, "<statusTexts>([V]*)</statusTexts>", "") $file = FileOpen($xml_file, 2) FileWrite($xml_file, $new_data) FileClose($file) thanks for your help!
jdelaney Posted May 14, 2012 Posted May 14, 2012 here...found the probelm, you can only delete child, when the object you delete from specifically has that child...so use the xpath to get the child, move up one level, and then delete it: $oXML=ObjCreate("Microsoft.XMLDOM") $stest = FileRead("c:usersjdelaneydesktoptest.xml") $oXML.LoadXML($stest) ConsoleWrite ( $oXML.xml & @CRLF) $oXMLChild1 = $oXML.selectSingleNode( "//activeText" ) $parent = $oXMLChild1.ParentNode $test = $parent.removeChild ( $oXMLChild1 ) ConsoleWrite ( $oXML.xml & @CRLF) IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
gcue Posted May 15, 2012 Author Posted May 15, 2012 awesome! works wonderfully! and i was able to add this at the end for the icing on the cake $oXML.Save($xml_file) thank you VERY much!!
gcue Posted May 18, 2012 Author Posted May 18, 2012 (edited) strange: this works fantastic on winxp (english version) but doesnt work on winxp (japanese version) the $oXML object gets created but fails in accesing the parentnode Func Remove_StatusTXT($xml_file) $oXML = ObjCreate("[color="#008080"]Microsoft.XMLDOM[/color]") If Not IsObj($oXML) Then MsgBox($msg_error, $selection_text, "Unable to create COM session to XML.") SetError(1) Return EndIf MsgBox(0, "", "1") $stest = FileRead($xml_file) $oXML.LoadXML($stest) MsgBox(0, "", "2") $oXMLChild1 = $oXML.SelectSingleNode("//statusTexts") MsgBox(0, "", "3") $parent = $oXMLChild1.ParentNode;error happens here MsgBox(0, "", "4") $test = $parent.RemoveChild($oXMLChild1);crash happens here MsgBox(0, "", "5") $oXML.Save($xml_file) MsgBox(0, "", "6") EndFunc ;==>Remove_StatusTXT ive also tried creating the object with each of these versions (obj gets created - but no luck with accessing parentNode): Func _CreateMSXMLObj() ; Creates a MSXML instance depending on the version installed on the system $xmlObj = ObjCreate("Msxml2.DOMDocument.6.0") ; Latest available, default in Vista If IsObj($xmlObj) Then ConsoleWrite("6" & @CRLF) Return $xmlObj EndIf $xmlObj = ObjCreate("Msxml2.DOMDocument.5.0") ; Office 2003 If IsObj($xmlObj) Then ConsoleWrite("5" & @CRLF) Return $xmlObj EndIf $xmlObj = ObjCreate("Msxml2.DOMDocument.4.0") If IsObj($xmlObj) Then ConsoleWrite("4" & @CRLF) Return $xmlObj EndIf $xmlObj = ObjCreate("Msxml2.DOMDocument.3.0") ; XP and w2k3 server If IsObj($xmlObj) Then ConsoleWrite("3" & @CRLF) Return $xmlObj EndIf $xmlObj = ObjCreate("Msxml2.DOMDocument.2.6") ; Win98 ME... If IsObj($xmlObj) Then ConsoleWrite("2.6" & @CRLF) Return $xmlObj EndIf Return 0 EndFunc ;==>_CreateMSXMLObj any ideas? Edited May 18, 2012 by gcue
gcue Posted May 31, 2012 Author Posted May 31, 2012 is there another way of editing a file (removing certain sections from it) without re-writing the file?
BrewManNH Posted May 31, 2012 Posted May 31, 2012 is there another way of editing a file (removing certain sections from it) without re-writing the file?No, once removed, it has to be rewritten, because it only exists in memory and any changes made will disappear once the memory is freed. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator
gcue Posted May 31, 2012 Author Posted May 31, 2012 my challenge remains: unable to edit the xml file to delete a node (works in english windows but not japanese windows)... and if i try to read the xml data and re-write the modified data - special characters are lost. i also tried inidelete - i think i saw someone using ini functions to edit xml files once.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now