lesmly Posted December 8, 2020 Share Posted December 8, 2020 Hi, First of all - English is not my forte and I apologize in advance for mistakes secondly - I always try to solve problems myself, but I have not dealt with xml and this problem is beyond my skills, although probably for someone with more skills it is a simple matter, so please help I've got XML and I want to extract data from it into arrays. First array is from <Main> section, next arrays are from <Alternative> section - number from <CountAlternative> informs how much "alternatives" are. In my xml file example: 1 main and 3 alternatives Arrays should look similar: a) Main Array Columns : Event(id) NumberPR InfoZ TITLE NumberS NumberT AS1 AS2 AS3 Video AE1 AE2 AE3 AE4 AE5 Copies CodeZ Band *[CD(id) CDN1 CDN2 CDN3 CDN4] * Number of sets [CD(id) CDN1 CDN1 CDN3 CDN4] for each <Event> is in <NumberOfCD> node - can be from 0 to who knows For simplicity, a maximum of 10 may be assumed, although it is better to check in XML file if possible. b) Alternative 1 Array Columns: Event(id) NumberPR InfoZ TITLE NumberS NumberT AS1 AS2 AS3 Video AE1 AE2 AE3 AE4 AE5 Copies CodeZ Band *[CD(id) CDN1 CDN2 CDN3 CDN4] c) ... If it were to be easier, it could be one array with all data. expandcollapse popup<?xml version="1.0"?> <AllList> <Info> <Article>1</Article> <Date>20201203</Date> <CountMain>4</CountMain> <CountAlternative>3</CountAlternative> <AlternativeAdded id="1"> <CountAlternativeEvents>2</CountAlternativeEvents> </AlternativeAdded> <AlternativeAdded id="2"> <CountAlternativeEvents>1</CountAlternativeEvents> </AlternativeAdded> <AlternativeAdded id="3"> <CountAlternativeEvents>1</CountAlternativeEvents> </AlternativeAdded> </Info> <Main> <Event id="1"> <NumberPR>00:00:00.00</NumberPR> <InfoZ>GB123456</InfoZ> <TITLE>Game</TITLE> <NumberS>00:00:00.00</NumberS> <NumberT>00:10:00.00</NumberT> <AddInfo> <AS1>D</AS1> <AS2>03FF</AS2> <AS3>1</AS3> </AddInfo> <Video>1</Video> <AddExtra> <AE1>1</AE1> <AE2>x</AE2> <AE3>0</AE3> <AE4>1</AE4> <AE5>0</AE5> </AddExtra> <Copies>30</Copies> <CodeZ>2A3B4E8C</CodeZ> <Band>1</Band> <NumberOfCD>2</NumberOfCD> <CD ID="1"> <CDN1>10:00:00.00</CDN1> <CDN2>00:00:10.00</CDN2> <CDN3>GAM_XCVFG_ASBCVD</CDN3> <CDN4>3</CDN4> </CD> <CD ID="2"> <CDN1>10:00:00.00</CDN1> <CDN2>00:00:10.00</CDN2> <CDN3>GAM_DEEDS_ASDF</CDN3> <CDN4>3</CDN4> </CD> </Event> <Event id="2"> <NumberPR>00:00:00.00</NumberPR> <InfoZ>BB123456</InfoZ> <TITLE>Book</TITLE> <NumberS>00:00:00.00</NumberS> <NumberT>00:10:00.00</NumberT> <AddInfo> <AS1>D</AS1> <AS2>03FF</AS2> <AS3>1</AS3> </AddInfo> <Video>1</Video> <AddExtra> <AE1>1</AE1> <AE2>x</AE2> <AE3>0</AE3> <AE4>1</AE4> <AE5>0</AE5> </AddExtra> <Copies>30</Copies> <CodeZ>2A3B4E8C</CodeZ> <Band>1</Band> <NumberOfCD>5</NumberOfCD> <CD ID="1"> <CDN1>10:00:10.00</CDN1> <CDN2>00:00:10.00</CDN2> <CDN3>BOO_XCVFG_ASBCVD</CDN3> <CDN4>3</CDN4> </CD> <CD ID="2"> <CDN1>10:00:20.00</CDN1> <CDN2>00:00:10.00</CDN2> <CDN3>BOO_EDDE_ASDF</CDN3> <CDN4>3</CDN4> </CD> <CD ID="3"> <CDN1>10:00:30.00</CDN1> <CDN2>00:00:10.00</CDN2> <CDN3>BOO_EDDF_ASDF</CDN3> <CDN4>3</CDN4> </CD> <CD ID="4"> <CDN1>10:00:40.00</CDN1> <CDN2>00:00:10.00</CDN2> <CDN3>BOO_EDDG_ASDF</CDN3> <CDN4>3</CDN4> </CD> <CD ID="5"> <CDN1>10:00:50.00</CDN1> <CDN2>00:00:10.00</CDN2> <CDN3>BOO_EDDH_ASDF</CDN3> <CDN4>3</CDN4> </CD> </Event> <Event id="3"> <NumberPR>00:00:00.00</NumberPR> <InfoZ>BB123456</InfoZ> <TITLE>Book</TITLE> <NumberS>00:00:00.00</NumberS> <NumberT>00:10:00.00</NumberT> <AddInfo> <AS1>D</AS1> <AS2>03FF</AS2> <AS3>1</AS3> </AddInfo> <Video>1</Video> <AddExtra> <AE1>1</AE1> <AE2>x</AE2> <AE3>0</AE3> <AE4>1</AE4> <AE5>0</AE5> </AddExtra> <Copies>30</Copies> <CodeZ>2A3B4E8C</CodeZ> <Band>1</Band> <NumberOfCD>5</NumberOfCD> <CD ID="1"> <CDN1>10:00:10.00</CDN1> <CDN2>00:00:10.00</CDN2> <CDN3>BOO_XCVFG_ASBCVD</CDN3> <CDN4>3</CDN4> </CD> <CD ID="2"> <CDN1>10:00:20.00</CDN1> <CDN2>00:00:10.00</CDN2> <CDN3>BOO_EDDE_ASDF</CDN3> <CDN4>3</CDN4> </CD> <CD ID="3"> <CDN1>10:00:30.00</CDN1> <CDN2>00:00:10.00</CDN2> <CDN3>BOO_EDDF_ASDF</CDN3> <CDN4>3</CDN4> </CD> <CD ID="4"> <CDN1>10:00:40.00</CDN1> <CDN2>00:00:10.00</CDN2> <CDN3>BOO_EDDG_ASDF</CDN3> <CDN4>3</CDN4> </CD> <CD ID="5"> <CDN1>10:00:50.00</CDN1> <CDN2>00:00:10.00</CDN2> <CDN3>BOO_EDDH_ASDF</CDN3> <CDN4>3</CDN4> </CD> </Event> <Event id="4"> <NumberPR>00:00:00.00</NumberPR> <InfoZ>BB123457</InfoZ> <TITLE>Book</TITLE> <NumberS>00:00:00.00</NumberS> <NumberT>00:10:00.00</NumberT> <AddInfo> <AS1>D</AS1> <AS2>03FF</AS2> <AS3>1</AS3> </AddInfo> <Video>1</Video> <AddExtra> <AE1>1</AE1> <AE2>x</AE2> <AE3>0</AE3> <AE4>1</AE4> <AE5>0</AE5> </AddExtra> <Copies>30</Copies> <CodeZ>2A3B4E8C</CodeZ> <Band>1</Band> <NumberOfCD>0</NumberOfCD> </Event> </Main> <Alternative> <AlternativeList id="1"> <Event id="1"> <NumberPR>00:00:00.00</NumberPR> <InfoZ>MB123456</InfoZ> <TITLE>Magazine</TITLE> <NumberS>00:00:00.00</NumberS> <NumberT>00:10:00.00</NumberT> <AddInfo> <AS1>D</AS1> <AS2>03FF</AS2> <AS3>1</AS3> </AddInfo> <Video>1</Video> <AddExtra> <AE1>1</AE1> <AE2>x</AE2> <AE3>0</AE3> <AE4>1</AE4> <AE5>0</AE5> </AddExtra> <Copies>30</Copies> <CodeZ>2A3B4E8C</CodeZ> <Band>1</Band> <NumberOfCD>0</NumberOfCD> </Event> <Event id="2"> <NumberPR>00:00:00.00</NumberPR> <InfoZ>NB123456</InfoZ> <TITLE>Magazine C</TITLE> <NumberS>00:00:00.00</NumberS> <NumberT>00:10:00.00</NumberT> <AddInfo> <AS1>D</AS1> <AS2>03FF</AS2> <AS3>1</AS3> </AddInfo> <Video>1</Video> <AddExtra> <AE1>1</AE1> <AE2>x</AE2> <AE3>0</AE3> <AE4>1</AE4> <AE5>0</AE5> </AddExtra> <Copies>30</Copies> <CodeZ>2A3B4E8C</CodeZ> <Band>1</Band> <NumberOfCD>1</NumberOfCD> <CD ID="1"> <CDN1>10:00:00.00</CDN1> <CDN2>00:00:10.00</CDN2> <CDN3>MAG_ESDRE_32322</CDN3> <CDN4>3</CDN4> </CD> </Event> </AlternativeList> <AlternativeList id="2"> <Event id="1"> <NumberPR>00:00:00.00</NumberPR> <InfoZ>MB123456</InfoZ> <TITLE>Magazine K</TITLE> <NumberS>00:00:00.00</NumberS> <NumberT>00:10:00.00</NumberT> <AddInfo> <AS1>D</AS1> <AS2>03FF</AS2> <AS3>1</AS3> </AddInfo> <Video>1</Video> <AddExtra> <AE1>1</AE1> <AE2>x</AE2> <AE3>0</AE3> <AE4>1</AE4> <AE5>0</AE5> </AddExtra> <Copies>30</Copies> <CodeZ>2A3B4E8C</CodeZ> <Band>1</Band> <NumberOfCD>1</NumberOfCD> <CD ID="1"> <CDN1>10:00:00.00</CDN1> <CDN2>00:00:10.00</CDN2> <CDN3>MAG_XCVFG_32346</CDN3> <CDN4>3</CDN4> </CD> </Event> </AlternativeList> <AlternativeList id="3"> <Event id="1"> <NumberPR>00:00:00.00</NumberPR> <InfoZ>MB123456</InfoZ> <TITLE>Magazine P</TITLE> <NumberS>00:00:00.00</NumberS> <NumberT>00:10:00.00</NumberT> <AddInfo> <AS1>D</AS1> <AS2>03FF</AS2> <AS3>1</AS3> </AddInfo> <Video>1</Video> <AddExtra> <AE1>1</AE1> <AE2>x</AE2> <AE3>0</AE3> <AE4>1</AE4> <AE5>0</AE5> </AddExtra> <Copies>30</Copies> <CodeZ>2A3B4E8C</CodeZ> <Band>1</Band> <NumberOfCD>1</NumberOfCD> <CD ID="1"> <CDN1>10:00:00.00</CDN1> <CDN2>00:00:10.00</CDN2> <CDN3>MAG_XCVFG_32349</CDN3> <CDN4>3</CDN4> </CD> </Event> </AlternativeList> </Alternative> </AllList> lm06.xml Link to comment Share on other sites More sharing options...
Nine Posted December 8, 2020 Share Posted December 8, 2020 (edited) Here a simple way to extract some of the tags : #include <Constants.au3> #include <Array.au3> Local Const $TAGS[] = ["<Event id=", "<NumberPR>", "<InfoZ>", "<TITLE>", "<NumberT>", "<AS1>"] Local $sPattern = _ArrayToString($TAGS) Local $aTag1D = StringRegExp(FileRead("Test.xml"), "(" & $sPattern & ")([^<>]*)", 3) Local $aTag2D = _Array_Resize($aTag1D, 2) _ArrayDisplay($aTag2D) Func _Array_Resize(ByRef $aArray1d, $iColumns, $iStart = Default) If IsKeyword($iStart) Then $iStart = 0 Local $iElements = UBound($aArray1d) - $iStart If Mod($iElements, $iColumns) <> 0 Then Return SetError(1, 0, False) Local $aArray2d[$iElements / $iColumns][$iColumns] For $i = 0 To $iElements - 1 $aArray2d[Floor($i / $iColumns)][Mod($i, $iColumns)] = $aArray1d[$i + $iStart] Next Return $aArray2d EndFunc ;==>_Array_Resize Once you got the 2D array, you can reformat at will. Edited December 8, 2020 by Nine Streamlined the code robertocm and lesmly 2 “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) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
TheXman Posted December 8, 2020 Share Posted December 8, 2020 (edited) Here's an example that processes the XML file using the XML UDF. This is not meant to be complete a solution. It is merely an example framework that you can learn from and build upon. I tried to document it well enough for you to get a general idea of what each block of code is doing. I also tried to give you an example of how you can process the different types of value structures that are in your XML file (single value, static number of sub values, dynamic number of sub values). As you said is preferable, the logic handles a variable number of CD entries from 0 to how ever many there are. It does so by creating an array of CD entries and placing that array as one of the array columns in the MainEvents array row. expandcollapse popup#include <Constants.au3> #include <Array.au3> #include <xml.au3> example() Func example() Const $XML_FILE = "lm06.xml" ;<-- Point to your xml file Local $oXmlDoc, $oNodes, $oNode Local $iCDCount Enum $MAIN_EVENTID, $MAIN_INFOZ, $MAIN_TITLE, $MAIN_AS1, $MAIN_AS2, $MAIN_AS3, $MAIN_CDARRAY, $MAIN_FLD_COUNT Local $aMainEvents[0][$MAIN_FLD_COUNT] Enum $CD_ID, $CD_CDN1, $CD_CDN2, $CD_CDN3, $CD_CDN4, $CD_FLD_COUNT Local $aCDs[0][$CD_FLD_COUNT] ;Create XML document object & load xml file $oXmlDoc = _XML_CreateDOMDocument() _XML_Load($oXmlDoc, $XML_FILE) If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "_XML_Load failed - @error = " & @error) ;Select //Main/Event nodes $oNodes = _XML_SelectNodes($oXmlDoc, '//Main/Event') If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "Error selecting //Main/Event nodes - @error = " & @error) ;Process //Main/Event nodes For $i = 0 To $oNodes.Length - 1 ;Add and populate an array row _ArrayAdd($aMainEvents, "") $oNode = $oNodes.Item($i) $aMainEvents[$i][$MAIN_EVENTID] = _XML_GetValue($oNode, '@id')[1] $aMainEvents[$i][$MAIN_INFOZ] = _XML_GetValue($oNode, 'InfoZ')[1] $aMainEvents[$i][$MAIN_TITLE] = _XML_GetValue($oNode, 'TITLE')[1] $aMainEvents[$i][$MAIN_AS1] = _XML_GetValue($oNode, 'AddInfo/AS1')[1] $aMainEvents[$i][$MAIN_AS2] = _XML_GetValue($oNode, 'AddInfo/AS2')[1] $aMainEvents[$i][$MAIN_AS3] = _XML_GetValue($oNode, 'AddInfo/AS3')[1] ;Create a dynamic array of CD entries and add the array to the main array row $iCDCount = _XML_GetValue($oNode, 'NumberOfCD')[1] ReDim $aCDs[0][$CD_FLD_COUNT] For $j = 0 To $iCDCount - 1 ;Add and populate a CD array row _ArrayAdd($aCDs, "") $aCDs[$j][$CD_ID] = _XML_GetValue($oNode, 'CD[' & $j + 1 & ']/@ID')[1] $aCDs[$j][$CD_CDN1] = _XML_GetValue($oNode, 'CD[' & $j + 1 & ']/CDN1')[1] $aCDs[$j][$CD_CDN2] = _XML_GetValue($oNode, 'CD[' & $j + 1 & ']/CDN2')[1] $aCDs[$j][$CD_CDN3] = _XML_GetValue($oNode, 'CD[' & $j + 1 & ']/CDN3')[1] $aCDs[$j][$CD_CDN4] = _XML_GetValue($oNode, 'CD[' & $j + 1 & ']/CDN4')[1] Next $aMainEvents[$i][$MAIN_CDARRAY] = $aCDs Next ;Display Main Events array _ArrayDisplay($aMainEvents, "Main Events", "", 0, Default, _ "ID|InfoZ|Title|AS1|AS2|AS3|CDARRAY") ;Display CD array of first Main Event row (as an example) _ArrayDisplay($aMainEvents[0][$MAIN_CDARRAY], "Main Events[0] CD Array", "", 0, Default, _ "ID|CDN1|CDN2|CDN3|CDN4") ;Display CD array of last Main Event row (there are none - empty array) _ArrayDisplay($aMainEvents[3][$MAIN_CDARRAY], "Main Events[3] CD Array", "", 0, Default, _ "ID|CDN1|CDN2|CDN3|CDN4") EndFunc Array Displays Using Your XML File: Edited December 8, 2020 by TheXman Standardized array logic FrancescoDiMuro, robertocm and lesmly 3 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
lesmly Posted December 9, 2020 Author Share Posted December 9, 2020 Thank you very much! Thanks to @Nine I have a quick solution to my problem, and thanks to @TheXman - I have a lot of work and study to get a more universal solution for the future. I couldn't imagine a better help, thank you.! Link to comment Share on other sites More sharing options...
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