Chad Posted February 24, 2011 Share Posted February 24, 2011 (edited) Hello, I'm trying to convert tabular data from a TXT file into XML. The project seems simple enough, use the XML template, fill in the blanks, and loop to fill out all the individual items contained in the source file. However for some reason it won't loop. I'm not quite sure what the issue is. I've included my code so far below. Hopefully if I can get this loop part to work, I can finish the project rather swiftly. expandcollapse popup#include <CSV.au3> #Include <File.au3> HotKeySet("{ESC}", "Terminate") #region Input TXT Columns Global $OrderID = 0 Global $OrderDate = 1 Global $ItemTitle = 2 Global $ItemBrand = 3 Global $ItemID = 4 Global $SKU = 5 Global $MPN = 6 Global $UPC = 7 Global $Quantity = 8 Global $Price = 9 Global $ShipCost = 10 Global $Tax = 11 Global $Total = 12 Global $BuyerFN = 13 Global $BuyerLN = 14 Global $ShipAdd = 15 Global $ShipCity = 16 Global $ShipState = 17 Global $ShipZip = 18 Global $ShipCount = 19 Global $Email = 20 Global $Phone = 21 Global $PayType = 22 Global $PpID = 23 Global $PayStat = 24 Global $ProtEdge = 25 Global $PendReas = 26 Global $OrdCom = 27 Global $SellID = 28 Global $BuyerID = 29 Global $FinalVal = 30 #endregion #region File Locations Global $SellerID = 2501 Global $sInput = "C:\Documents and Settings\Pricefalls\Desktop\Chad\Projects\CSVtoXML\sample_order_sheet.txt" Global $sOutput = "C:\Documents and Settings\Pricefalls\Desktop\Chad\Projects\CSVtoXML\" & $SellerID & "_SalesSheet_" & @YEAR & @MON & @MDAY & ".txt" #endregion #region XML Header Construct Global $XMLFile = FileOpen($sOutput,129) Global $TimeStamp = @YEAR & "-" & @MON & "-" & @MDAY & "T" & @HOUR & ":" & @MIN & ":" & @SEC & ".000Z" FileWriteLine($XMLFile, '<?xml version="1.0" encoding="UTF-8"?>' ) FileWriteLine($XMLFile, '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' ) FileWriteLine($XMLFile, '<soapenv:Header>' ) FileWriteLine($XMLFile, ' <ebl:RequesterCredentials soapenv:mustUnderstand="0" xmlns:ns="urn:ebay:apis:eBLBaseComponents" xmlns:ebl="urn:ebay:apis:eBLBaseComponents">' ) FileWriteLine($XMLFile, ' <ebl:NotificationSignature xmlns:ebl="urn:ebay:apis:eBLBaseComponents">O8vGA/KYBSkJxHHeBgsmww==</ebl:NotificationSignature>' ) FileWriteLine($XMLFile, ' </ebl:RequesterCredentials>' ) FileWriteLine($XMLFile, '</soapenv:Header>' ) FileWriteLine($XMLFile, @CRLF ) FileWriteLine($XMLFile, '<soapenv:Body>' ) FileWriteLine($XMLFile, ' <GetItemTransactionsResponse xmlns="urn:ebay:apis:eBLBaseComponents">' ) FileWriteLine($XMLFile, ' <Timestamp>' & $TimeStamp & '</Timestamp>' ) FileWriteLine($XMLFile, ' <Ack>Success</Ack>' ) FileWriteLine($XMLFile, ' <CorrelationID></CorrelationID>' ) FileWriteLine($XMLFile, ' <Version></Version>' ) FileWriteLine($XMLFile, ' <Build></Build>' ) FileWriteLine($XMLFile, ' <NotificationEventName></NotificationEventName>' ) FileWriteLine($XMLFile, ' <RecipientUserID>' & $SellerID & '</RecipientUserID>' ) FileWriteLine($XMLFile, ' <EIASToken>N/A</EIASToken>' ) FileWriteLine($XMLFile, ' <PaginationResult>' ) FileWriteLine($XMLFile, ' <TotalNumberOfPages>1</TotalNumberOfPages>' ) FileWriteLine($XMLFile, ' <TotalNumberOfEntries>1</TotalNumberOfEntries>' ) FileWriteLine($XMLFile, ' </PaginationResult>' ) FileWriteLine($XMLFile, ' <HasMoreTransactions>false</HasMoreTransactions>' ) FileWriteLine($XMLFile, ' <TransactionsPerPage>N/A</TransactionsPerPage>' ) FileWriteLine($XMLFile, ' <PageNumber>N/A</PageNumber>' ) FileWriteLine($XMLFile, ' <ReturnedTransactionCountActual>N/A</ReturnedTransactionCountActual>' ) #endregion #region Items $sFile = _ParseCSV($sInput, @TAB) $iCount = Ubound($sFile)-1 $cCount = Ubound($sFile,2) #endregion #region Create XML File For $i = 1 to $iCount Step -1 FileWriteLine($XMLFile, ' <Item>' ) FileWriteLine($XMLFile, ' <AutoPay>false</AutoPay>' ) FileWriteLine($XMLFile, ' <BuyItNowPrice currencyID=USD>0.00</BuyItNowPrice>' ) FileWriteLine($XMLFile, ' <Currency>USD</Currency>' ) FileWriteLine($XMLFile, ' <ItemID>' & $sFile[$i][$ItemID] & '</ItemID>' ) FileWriteLine($XMLFile, ' <ListingDetails>' ) FileWriteLine($XMLFile, ' <StartTime>N/A</StartTime>' ) FileWriteLine($XMLFile, ' <EndTime>N/A</EndTime>' ) FileWriteLine($XMLFile, ' <ViewItemURL>http://www.pricefalls.com/detail.ctrl.php?item_id=' & $sFile[$i][$ItemID] & '</ViewItemURL>' ) FileWriteLine($XMLFile, ' <ViewItemURLForNaturalSearch>http://www.pricefalls.com/detail.ctrl.php?item_id=' & $sFile[$i][$ItemID] & '</ViewItemURLForNaturalSearch>' ) FileWriteLine($XMLFile, ' </ListingDetails>' ) FileWriteLine($XMLFile, ' <ListingType>N/A</ListingType>' ) FileWriteLine($XMLFile, ' <PaymentMethods>' & $sFile[$i][$PayType] & '</PaymentMethods>' ) FileWriteLine($XMLFile, ' <PrimaryCategory>' ) FileWriteLine($XMLFile, ' <CategoryID>N/A</CategoryID>' ) FileWriteLine($XMLFile, ' </PrimaryCategory>' ) FileWriteLine($XMLFile, ' <PrivateListing>false</PrivateListing>' ) FileWriteLine($XMLFile, ' <SecondaryCategory>' ) FileWriteLine($XMLFile, ' <SecondaryCategory>' ) FileWriteLine($XMLFile, ' </SecondaryCategory>' ) FileWriteLine($XMLFile, ' <Seller>' ) FileWriteLine($XMLFile, ' <AboutMePage>false</AboutMePage>' ) FileWriteLine($XMLFile, ' <EIASToken>N/A</EIASToken>' ) FileWriteLine($XMLFile, ' <Email>N/A</Email>' ) FileWriteLine($XMLFile, ' <FeedbackScore>N/A</FeedbackScore>' ) FileWriteLine($XMLFile, ' <PositiveFeedbackPercent>N/A</PositiveFeedbackPercent>' ) FileWriteLine($XMLFile, ' <FeedbackPrivate>false</FeedbackPrivate>' ) FileWriteLine($XMLFile, ' <FeedbackRatingStar>N/A</FeedbackRatingStar>' ) FileWriteLine($XMLFile, ' <IDVerified>false</IDVerified>' ) FileWriteLine($XMLFile, ' <eBayGoodStanding>N/A</eBayGoodStanding>' ) FileWriteLine($XMLFile, ' <NewUser>N/A</NewUser>' ) FileWriteLine($XMLFile, ' <RegistrationDate>N/A</RegistrationDate>' ) FileWriteLine($XMLFile, ' <Site>US</Site>' ) FileWriteLine($XMLFile, ' <Status>Confirmed</Status>' ) FileWriteLine($XMLFile, ' <UserID>' & $sFile[$i][$SellID] &'</UserID>' ) FileWriteLine($XMLFile, ' <UserIDChanged>N/A</UserIDChanged>' ) FileWriteLine($XMLFile, ' <UserIDLastChanged>N/A</UserIDLastChanged>' ) FileWriteLine($XMLFile, ' <VATStatus>NoVATTax</VATStatus>' ) FileWriteLine($XMLFile, ' <SellerInfo>' ) FileWriteLine($XMLFile, ' <AllowPaymentEdit>true</AllowPaymentEdit>' ) FileWriteLine($XMLFile, ' <CheckoutEnabled>true</CheckoutEnabled>' ) FileWriteLine($XMLFile, ' <CIPBankAccountStored>false</CIPBankAccountStored>' ) FileWriteLine($XMLFile, ' <GoodStanding>true</GoodStanding>' ) FileWriteLine($XMLFile, ' <LiveAuctionAuthorized>false</LiveAuctionAuthorized>' ) FileWriteLine($XMLFile, ' <MerchandizingPref>OptIn</MerchandizingPref>' ) FileWriteLine($XMLFile, ' <QualifiesForB2BVAT>false</QualifiesForB2BVAT>' ) FileWriteLine($XMLFile, ' <StoreOwner>true</StoreOwner>' ) FileWriteLine($XMLFile, ' <SafePaymentExempt>false</SafePaymentExempt>' ) FileWriteLine($XMLFile, ' </SellerInfo>' ) FileWriteLine($XMLFile, ' </Seller>' ) FileWriteLine($XMLFile, ' <SellingStatus>' ) FileWriteLine($XMLFile, ' <ConvertedCurrentPrice currencyID="USD">' & $sFile[$i][$Total] & '</ConvertedCurrentPrice>' ) FileWriteLine($XMLFile, ' <CurrentPrice currencyID="USD">' & $sFile[$i][$Total] & '</CurrentPrice>' ) Next #endregion #region Functions Func Terminate() Exit 0 EndFunc ;==>Terminate #endregion Exit Any help is appreciated. Best, Chad EDIT: Subject should say 'Loop' not 'Look'. Silly fingers... Edited February 24, 2011 by Chad Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 24, 2011 Moderators Share Posted February 24, 2011 Chad,This lineFor $i = 1 to $iCount Step -1will cause problems in your loop as you are reducing the start count and so you are looking to read elements [1], [0], [-1], etc - little wonder it fails. If you change it to read:For $i = 1 to $iCountYou should find it loops as you expect.You should also close the file with a FileClose line somewhere. M23  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area  Link to comment Share on other sites More sharing options...
Chad Posted February 24, 2011 Author Share Posted February 24, 2011 Chad, This line For $i = 1 to $iCount Step -1 will cause problems in your loop as you are reducing the start count and so you are looking to read elements [1], [0], [-1], etc - little wonder it fails. If you change it to read: For $i = 1 to $iCount You should find it loops as you expect. You should also close the file with a FileClose line somewhere. M23 Thanks so much, I actually did just that seconds after I posted. Derp. Works wonders now, thank you very much. -Chad Link to comment Share on other sites More sharing options...
PsaltyDS Posted February 24, 2011 Share Posted February 24, 2011 (edited) I would do it more like this: expandcollapse popupGlobal $sHeader = '<?xml version="1.0" encoding="UTF-8"?>' & @CRLF & _ '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' & @CRLF & _ '<soapenv:Header>' & @CRLF & _ ' <ebl:RequesterCredentials soapenv:mustUnderstand="0" xmlns:ns="urn:ebay:apis:eBLBaseComponents" xmlns:ebl="urn:ebay:apis:eBLBaseComponents">' & @CRLF & _ ' <ebl:NotificationSignature xmlns:ebl="urn:ebay:apis:eBLBaseComponents">O8vGA/KYBSkJxHHeBgsmww==</ebl:NotificationSignature>' & @CRLF & _ ' </ebl:RequesterCredentials>' & @CRLF & _ '</soapenv:Header>' & @CRLF & _ @CRLF & @CRLF & _ '<soapenv:Body>' & @CRLF & _ ' <GetItemTransactionsResponse xmlns="urn:ebay:apis:eBLBaseComponents">' & @CRLF & _ ' <Timestamp>~~TimeStamp</Timestamp>' & @CRLF & _ ' <Ack>Success</Ack>' & @CRLF & _ ' <CorrelationID></CorrelationID>' & @CRLF & _ ' <Version></Version>' & @CRLF & _ ' <Build></Build>' & @CRLF & _ ' <NotificationEventName></NotificationEventName>' & @CRLF & _ ' <RecipientUserID>~~SellerID~~</RecipientUserID>' & @CRLF & _ ' <EIASToken>N/A</EIASToken>' & @CRLF & _ ' <PaginationResult>' & @CRLF & _ ' <TotalNumberOfPages>1</TotalNumberOfPages>' & @CRLF & _ ' <TotalNumberOfEntries>1</TotalNumberOfEntries>' & @CRLF & _ ' </PaginationResult>' & @CRLF & _ ' <HasMoreTransactions>false</HasMoreTransactions>' & @CRLF & _ ' <TransactionsPerPage>N/A</TransactionsPerPage>' & @CRLF & _ ' <PageNumber>N/A</PageNumber>' & @CRLF & _ ' <ReturnedTransactionCountActual>N/A</ReturnedTransactionCountActual>' & @CRLF Global $sItem = ' <Item>' & @CRLF & _ ' <AutoPay>false</AutoPay>' & @CRLF & _ ' <BuyItNowPrice currencyID=USD>0.00</BuyItNowPrice>' & @CRLF & _ ' <Currency>USD</Currency>' & @CRLF & _ ' <ItemID>~~ItemID~~</ItemID>' & @CRLF & _ ' <ListingDetails>' & @CRLF & _ ' <StartTime>N/A</StartTime>' & @CRLF & _ ' <EndTime>N/A</EndTime>' & @CRLF & _ ' <ViewItemURL>http://www.pricefalls.com/detail.ctrl.php?item_id=~~ItemID~~</ViewItemURL>' & @CRLF & _ ' <ViewItemURLForNaturalSearch>http://www.pricefalls.com/detail.ctrl.php?item_id=~~ItemID~~</ViewItemURLForNaturalSearch>' & @CRLF & _ ' </ListingDetails>' & @CRLF & _ ' <ListingType>N/A</ListingType>' & @CRLF & _ ' <PaymentMethods>~~PayType~~</PaymentMethods>' & @CRLF & _ ' <PrimaryCategory>' & @CRLF & _ ' <CategoryID>N/A</CategoryID>' & @CRLF & _ ' </PrimaryCategory>' & @CRLF & _ ' <PrivateListing>false</PrivateListing>' & @CRLF & _ ' <SecondaryCategory>' & @CRLF & _ ' <SecondaryCategory>' & @CRLF & _ ' </SecondaryCategory>' & @CRLF & _ ' <Seller>' & @CRLF & _ ' <AboutMePage>false</AboutMePage>' & @CRLF & _ ' <EIASToken>N/A</EIASToken>' & @CRLF & _ ' <Email>N/A</Email>' & @CRLF & _ ' <FeedbackScore>N/A</FeedbackScore>' & @CRLF & _ ' <PositiveFeedbackPercent>N/A</PositiveFeedbackPercent>' & @CRLF & _ ' <FeedbackPrivate>false</FeedbackPrivate>' & @CRLF & _ ' <FeedbackRatingStar>N/A</FeedbackRatingStar>' & @CRLF & _ ' <IDVerified>false</IDVerified>' & @CRLF & _ ' <eBayGoodStanding>N/A</eBayGoodStanding>' & @CRLF & _ ' <NewUser>N/A</NewUser>' & @CRLF & _ ' <RegistrationDate>N/A</RegistrationDate>' & @CRLF & _ ' <Site>US</Site>' & @CRLF & _ ' <Status>Confirmed</Status>' & @CRLF & _ ' <UserID>~~SellID~~</UserID>' & @CRLF & _ ' <UserIDChanged>N/A</UserIDChanged>' & @CRLF & _ ' <UserIDLastChanged>N/A</UserIDLastChanged>' & @CRLF & _ ' <VATStatus>NoVATTax</VATStatus>' & @CRLF & _ ' <SellerInfo>' & @CRLF & _ ' <AllowPaymentEdit>true</AllowPaymentEdit>' & @CRLF & _ ' <CheckoutEnabled>true</CheckoutEnabled>' & @CRLF & _ ' <CIPBankAccountStored>false</CIPBankAccountStored>' & @CRLF & _ ' <GoodStanding>true</GoodStanding>' & @CRLF & _ ' <LiveAuctionAuthorized>false</LiveAuctionAuthorized>' & @CRLF & _ ' <MerchandizingPref>OptIn</MerchandizingPref>' & @CRLF & _ ' <QualifiesForB2BVAT>false</QualifiesForB2BVAT>' & @CRLF & _ ' <StoreOwner>true</StoreOwner>' & @CRLF & _ ' <SafePaymentExempt>false</SafePaymentExempt>' & @CRLF & _ ' </SellerInfo>' & @CRLF & _ ' </Seller>' & @CRLF & _ ' <SellingStatus>' & @CRLF & _ ' <ConvertedCurrentPrice currencyID="USD">~~Total~~</ConvertedCurrentPrice>' & @CRLF & _ ' <CurrentPrice currencyID="USD">~~Total~~</CurrentPrice>' & @CRLF #region XML Header Construct Global $hXMLFile = FileOpen($sOutput, 129) Global $TimeStamp = @YEAR & "-" & @MON & "-" & @MDAY & "T" & @HOUR & ":" & @MIN & ":" & @SEC & ".000Z" Global $sData = StringReplace($sHeader, "~~TimeStamp~~", $TimeStamp) $sData = StringReplace($sData, "~~SellerID~~", $SellerID) FileWrite($hXMLFile, $sData) #endregion XML Header Construct #region Items Global $aFile = _ParseCSV($sInput, @TAB) If IsArray($aFile) And (UBound($aFile, 0) = 2) Then MsgBox(64, "Debug", "Array returned by _ParseCSV() = $aFile[" & UBound($aFile, 1) & "][" & UBound($aFile, 2) & "]") Else MsgBox(16, "Debug", "Error: $aFile returned by _ParseCSV() is invalid.") Exit EndIf $iCount = UBound($aFile) - 1 $cCount = UBound($aFile, 2) #endregion Items #region Create XML File For $i = 1 To $iCount $sData = StringReplace($sItem, "~~ItemID~~", $aFile[$i][$ItemID]) $sData = StringReplace($sData, "~~PayType~~", $aFile[$i][$PayType]) $sData = StringReplace($sData, "~~SellID~~", $aFile[$i][$SellID]) $sData = StringReplace($sData, "~~Total~~", $aFile[$i][$Total]) FileWrite($hXMLFile, $sData) Next FileClose($hXMLFile) #endregion Create XML File Note there is a debug check to see that you get expected array back from _ParseCSV(). Edited February 24, 2011 by PsaltyDS Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law 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