apbonn Posted January 27, 2020 Posted January 27, 2020 Hi, i ve got a file (file.xml) where i want to find content between a tag. The file.xml looks like this: <package> <category name="test"> <resource>test1</resource> <resource>test2</resource> <resource>test3</resource> <resource>test4</resource> <resource>test5</resource> </category> </package> But it can also look different, but the content i want to know is always between <resource> tags How do i do that, im trying this: #include <Constants.au3> #include <Array.au3> #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> #include <File.au3> #include <AutoItConstants.au3> #include <Guiconstants.au3> #include <FileConstants.au3> $f = FileOpen("file.xml") $content = FileRead($f) ) $arr = StringRegExp($content,"<resource>(.|\n)*?<\/resource>",3) for $i = 0 to UBound($arr) -1 msgbox(0,"",$arr[$i]) Next FileClose($f) But it doesnt work. Does anybody has an idea?
FrancescoDiMuro Posted January 27, 2020 Posted January 27, 2020 Hi @apbonn, and welcome to the AutoIt forums Try this pattern and let us know: '<resource>([^<]*)<\/resource>' Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette
FrancescoDiMuro Posted January 27, 2020 Posted January 27, 2020 Glad I could be of help Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette
Deye Posted January 27, 2020 Posted January 27, 2020 (edited) And for extra bookmarking #include <Array.au3> Local $data = '' _ & '<package>' _ & @LF & ' <category name="test">' _ & @LF & ' <resource>test1</resource>' _ & @LF & ' <resource>test2</resource>' _ & @LF & ' <resource>test3</resource>' _ & @LF & ' <resource>test4</resource>' _ & @LF & ' <resource>test5</resource>' _ & @LF & ' </category>' _ & @LF & '</package>' $arr = StringRegExp($data, "[\w\d]+(?=</)", 3) _ArrayDisplay($arr) Deye Edited January 27, 2020 by Deye
apbonn Posted February 3, 2020 Author Posted February 3, 2020 Hey guys new, Problem Im looing for something new Sample content <tag name="sample"/> <tag name="main"/> </tags> <group_id value="XMB_DEF_20502"/> <aspect_ratio max="0.9992224" min="0.9992224" opt="0.9992224"/> <colorization_supported value="false"/> And i'm searching for the value of group_id, so the output should look like this XMB_DEF_20502 Im trying do to this like this: '<group_id value="([^<]*)"\/>' But the result is empty, do you have any idea ?
Nine Posted February 3, 2020 Posted February 3, 2020 '<group_id value="([^"]*)' Try this “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
apbonn Posted February 3, 2020 Author Posted February 3, 2020 6 minutes ago, Nine said: '<group_id value="([^"]*)' Try this Still empty
Nine Posted February 3, 2020 Posted February 3, 2020 Worked for me. Show your code, maybe there is an error somewhere. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
apbonn Posted February 3, 2020 Author Posted February 3, 2020 $findGroup = FileOpen('test.xml') $contentXML = FileRead($findGroup) $groupID = StringRegExp($contentXML,'<group_id value="([^"]*)',3) MsgBox($MB_SYSTEMMODAL, "groupID", $groupID) FileClose($findGroup) test.xml <?xml version="1.0" ?> <decoeffect> <tags> <tag name="category.test"/> <tag name="exclusiveproduct.test"/> <tag name="language.default"/> <tag name="page.test"/> <tag name="page.test"/> <tag name="page.inner"/> <tag name="product.default"/> <tag name="theme.test"/> </tags> <group_id value="B00003"/> <aspect_ratio max="1.6222" min="1.3506" opt="1.4864"/> <colorization_supported value="false"/> <shadow_supported value="false"/> <shadow_colorization_supported value="false"/> <relative_padding bottom="0.1023" left="0.0993" right="0.1235" top="0.1316"/> <stretch_and_compress_constraints horz_max="200.0" horz_min="0.1" vert_max="200.0" vert_min="0.1"/> <overlay tile_usage="2"/> </decoeffect>
Nine Posted February 3, 2020 Posted February 3, 2020 $groupID is an array... “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
apbonn Posted February 3, 2020 Author Posted February 3, 2020 oh man.. im sorry. but how do i find my value?
Nine Posted February 3, 2020 Posted February 3, 2020 $groupID = StringRegExp($contentXML,'<group_id value="([^"]*)',3) MsgBox($MB_SYSTEMMODAL, "groupID", $groupID[0]) or $groupID = StringRegExp($contentXML,'<group_id value="([^"]*)',3)[0] MsgBox($MB_SYSTEMMODAL, "groupID", $groupID) “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
apbonn Posted February 3, 2020 Author Posted February 3, 2020 big big thanks so one last question, but it is a different topic. if got a string that looks like this: IPL/decoeffects/birthday/tripplemint But it can have more or less / , like IPL/decoeffects/birthday/tripplemint/IPL/decoeffects/birthday/tripplemint IPL/decoeffects/birthday/tripplemint/test/test IPL/decoeffects What i want is the the string without the last word, for example IPL/decoeffects/birthday/tripplemint/IPL/decoeffects/birthday/tripplemint IPL/decoeffects/birthday/tripplemint/test/contnent IPL/decoeffects RESULT = IPL/decoeffects/birthday/tripplemint/IPL/decoeffects/birthday/ IPL/decoeffects/birthday/tripplemint/test/ IPL/ Do you know how to do that?
Nine Posted February 3, 2020 Posted February 3, 2020 Try this : #include <Array.au3> ; this could be the result of an FileReadToArray () Local $aArray [] = _ ["IPL/decoeffects/birthday/tripplemint/IPL/decoeffects/birthday/tripplemint", _ "IPL/decoeffects/birthday/tripplemint/test/contnent" , _ "IPL/decoeffects"] For $i = 0 to UBound($aArray)-1 $aArray[$i] = StringTrimRight($aArray[$i],StringLen($aArray[$i])-StringInStr($aArray[$i],"/",0,-1)) Next _ArrayDisplay ($aArray) “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
apbonn Posted February 3, 2020 Author Posted February 3, 2020 The content to be changed is not an array , it is just string.
Nine Posted February 3, 2020 Posted February 3, 2020 Each line must be terminated by @CRLF ? If so StringSplit to array before... “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
Subz Posted February 3, 2020 Posted February 3, 2020 You could also use XML dom to get your xml values and use StringSplit to get the values of your string, example: expandcollapse popup#cs Data.xml <?xml version="1.0" ?> <decoeffect> <package> <category name="test"> <resource>test1</resource> <resource>test2</resource> <resource>test3</resource> <resource>test4</resource> <resource>test5</resource> </category> </package> <tags> <tag name="category.test"/> <tag name="exclusiveproduct.test"/> <tag name="language.default"/> <tag name="page.test"/> <tag name="page.test"/> <tag name="page.inner"/> <tag name="product.default"/> <tag name="theme.test"/> </tags> <group_id value="B00003"/> <aspect_ratio max="1.6222" min="1.3506" opt="1.4864"/> <colorization_supported value="false"/> <shadow_supported value="false"/> <shadow_colorization_supported value="false"/> <relative_padding bottom="0.1023" left="0.0993" right="0.1235" top="0.1316"/> <stretch_and_compress_constraints horz_max="200.0" horz_min="0.1" vert_max="200.0" vert_min="0.1"/> <overlay tile_usage="2"/> </decoeffect> #ce #include <Array.au3> Local $oXMLDoc = ObjCreate("MSXML2.DOMDocument") $oXMLDoc.validateOnParse = True $oXMLDoc.load(@ScriptDir & "\Data.xml") ;~ Example 1 Local $oResources = $oXMLDoc.selectNodes('//decoeffect/package/category[@name="test"]/resource') If IsObj($oResources) Then For $oResource In $oResources ConsoleWrite("Resource: " & $oResource.text & @CRLF) Next EndIf ;~ Example 2 Local $oGroupId = $oXMLDoc.selectSingleNode("//decoeffect/group_id") If IsObj($oGroupId) Then ConsoleWrite("Group Id : " & $oGroupId.getAttribute("value") & @CRLF) EndIf ;~ Example 3 Local $aResult, $sResult = "", $sString = "IPL/decoeffects/birthday/tripplemint/IPL/decoeffects/birthday/tripplemint" & @CRLF $sString &= "IPL/decoeffects/birthday/tripplemint/test/test" & @CRLF $sString &= "IPL/decoeffects" & @CRLF $sString &= "IPL" Local $aString = StringSplit($sString, @CRLF, 1) For $i = 1 To $aString[0] $aResult = StringSplit($aString[$i], "/", 2) $sResult &= (UBound($aResult) - 2) >= 0 ? _ArrayToString($aResult, "/", -1, UBound($aResult) - 2) & "/" & @CRLF : $aResult[0] & "/" & @CRLF Next ConsoleWrite($sResult & @CRLF)
apbonn Posted February 3, 2020 Author Posted February 3, 2020 (edited) maybe im getting this wrong $x = 'test/content/to/check' ; the part i dont get $result = 'test/content/to/' only the $x is changing. this has nothing to do with the previous xml part Edited February 3, 2020 by apbonn
Nine Posted February 3, 2020 Posted February 3, 2020 (edited) Local $x = 'test/content/to/check' MsgBox (0,"",StringTrimRight($x,StringLen($x)-StringInStr($x,"/",0,-1))) If it is only one line then... Edited February 3, 2020 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
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