Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 12/27/2020 in all areas

  1. 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. #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:
    1 point
  2. 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.
    1 point
×
×
  • Create New...