SimpleXML: XML to CSV for multi-level XML files

Wkarcdfmzymsm

New Member
I have a multi-level XML file that I'm trying to convert to a CSV using SimpleXML. The file looks like something like this:\[code\] <Products> <Product> <StyleCode>Product Number 1</StyleCode> <Name>Name of Product 1</Name> <Description>Product 1 Is Great</Description> <Categories> <Category>SS-TEES</Category> </Categories> <Items> <Item> <Code>1111122223333444</Code> <Barcode>1111122223333444</Barcode> <Size>2XL</Size> <ShortColour>BLK</ShortColour> <Colour>BLACK</Colour> <Price>39.95</Price> <StockOnHand>6</StockOnHand> </Item> <Item> <Code>0000001427002001</Code> <Barcode>0000001427002001</Barcode> <Size>M</Size> <ShortColour>BLK</ShortColour> <Colour>BLACK</Colour> <Price>39.95</Price> <PriceSpecial>0</PriceSpecial> <StockOnHand>2</StockOnHand> </Item> </Items> </Product> <Product> ....... </Product></Products>\[/code\]I am having trouble getting this into a CSV. I have succeeded on simple 1 level XML files but this is proving some hassle.Each ITEM should have it's own line on the CSV containing the information from it's parent PRODUCT.So the first line of the CSV would be\[code\]Product Number 1, Name of Product 1, Product 1 Is Great, SS-TEES, 111222333, 111222333, 2XL, BLK, Black, 39.95, 6\[/code\]Line 2 would then be\[code\]Product Number 1, Name of Product 1, Product 1 Is Great, SS-TEES, 00000142, 00001427, M, Blk, Black, 39.95, 2\[/code\]And so on.I'm thinking I need to store the products 1st children in a variable (stylecode,name,description etc), and then while there is an item, print the 1st child variable and then the item but I'm not really sure how to go about it. The structure is consistent throughout the entire document however there are blank fields.
 
Back
Top