tbodine88 Posted February 2, 2010 Share Posted February 2, 2010 I am trying to convert XML to CSV. The XML I am converting is a database where there are rows with different values inside each row. I can't seem to figure out how to do it. Here is my XML <code> <?xml version="1.0" encoding="UTF-8"?> <KLetter xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="tbMessages.xsd"> <SANDB> <RowCount>2</RowCount> <AobjTable alias="Things" totalRecords="1187"> <numObj key="yes" alias="Alarm Point" type="xs:unsignedShort">2366</numObj> <GrafLink alias="Has Graphic" type="xs:boolean">false</GrafLink> <VidLink alias="Has Video" type="xs:boolean">false</VidLink> <label alias="Descriptor" type="xs:unsignedByte" rtname="DescStrs" desc="INTRUSION">86</label> <OpDeny alias="Operator Access" type="xs:unsignedByte" rtname="AllwDStr" desc="Not Allowed">1</OpDeny> <GrpOk alias="Group Access" type="xs:unsignedByte" rtname="AllwDStr" desc="Allowed">0</GrpOk> <AcknAuto alias="Acknowledgment" type="xs:unsignedByte" rtname="ReqrDStr" desc="Required">0</AcknAuto> <FolowAl alias="Subseqnt Alarm" type="xs:unsignedByte" rtname="AllwRStr" desc="Not Allowed">0</FolowAl> <Accby2 alias="Two Opr. Access" type="xs:unsignedByte" rtname="ReqrRStr" desc="Not Required">0</Accby2> <SecBy2 alias="Two Opr. Secure" type="xs:unsignedByte" rtname="ReqrRStr" desc="Not Required">0</SecBy2> <ResSenA alias="Sensor Restore Alarm" type="xs:boolean">false</ResSenA> <Func alias="Point Type" type="xs:unsignedByte" rtname="Func" desc="Standard Alarm">1</Func> <Priorty alias="Base Priority" type="xs:unsignedByte">100</Priorty> <Loctn alias="Location ID" type="xs:unsignedShort" rtname="LocStrsT" desc="B-230 VAULT 137 ">445</Loctn> </AobjTable> <AobjTable alias="Things" totalRecords="1187"> <numObj key="yes" alias="Alarm Point" type="xs:unsignedShort">2381</numObj> <GrafLink alias="Has Graphic" type="xs:boolean">false</GrafLink> <VidLink alias="Has Video" type="xs:boolean">false</VidLink> <label alias="Descriptor" type="xs:unsignedByte" rtname="DescStrs" desc="SECURE/ACCESS">131</label> <DisOp alias="Operator Disable" type="xs:unsignedByte" rtname="AllwDStr" desc="Not Allowed">1</DisOp> <PolSen alias="Sens Polarity" type="xs:unsignedByte" rtname="RMACstrs" desc="SECURE = Access">0</PolSen> <TarPts alias="Target Points" type="xs:unsignedByte" rtname="GAOprStT" desc="Number Range">0</TarPts> <Tgls alias="Event Toggles?" type="xs:boolean">false</Tgls> <TimOc alias="Occupied Time" type="xs:unsignedByte">0</TimOc> <Tim1 alias="Timebase" type="xs:unsignedByte" rtname="Tim2" desc="Seconds">0</Tim1> <ExDly alias="Exit Delay" type="xs:unsignedByte">20</ExDly> <Tim2 alias="Timebase" type="xs:unsignedByte" rtname="Tim2" desc="Seconds">0</Tim2> <LenAcc alias="Access Depth" type="xs:unsignedByte">4</LenAcc> <Func alias="Point Type" type="xs:unsignedByte" rtname="Func" desc="Group Access">7</Func> <Priorty alias="Base Priority" type="xs:unsignedByte">100</Priorty> <Loctn alias="Location ID" type="xs:unsignedShort" rtname="LocStrsT" desc="B-5905 FLT KITC ATM">446</Loctn> </AobjTable> </SANDB> </KLetter> </code> Here is my sample program <code> #include <_XMLDomWrapper.au3> _XMLFileOpen("example.xml") If @error Then ConsoleWrite("exmple.xml " & _XMLError("") & @CRLF) Exit EndIf $path = "/KLetter/SANDB" $children = _XMLGetChildNodes($path) for $i = 1 to UBound($children) -1 ConsoleWrite( $children[$i] & @CRLF ) $kids = _XMLGetChildNodes($path & "/" & $children[$i]) for $j = 1 to UBound( $kids ) - 1 ConsoleWrite( " " & $kids[$j] & ",") next ConsoleWrite(@CRLF) Next </code> This gives me this output <code> AobjTable numObj, GrafLink, VidLink, label, OpDeny, GrpOk, AcknAuto, FolowAl, Accby2, SecBy2, ResSenA, Func, Priorty, Loctn, AobjTable numObj, GrafLink, VidLink, label, OpDeny, GrpOk, AcknAuto, FolowAl, Accby2, SecBy2, ResSenA, Func, Priorty, Loctn, </code> But I want this output: <code> AobjTable numObj, GrafLink, VidLink, label, OpDeny, GrpOk, AcknAuto, FolowAl, Accby2, SecBy2, ResSenA, Func, Priorty, Loctn, AobjTable numObj, GrafLink, VidLink, label, DisOp, PolSen, TarPts, Tgls, TimOc, Tim1, ExDly, Tim2, LenAcc, Func, Priorty, Loctn, </code> But I really need to figure out how I can get this Output: <code> AobjTable numObj, GrafLink, VidLink, label, OpDeny, GrpOk, AcknAuto, FolowAl, Accby2, SecBy2, ResSenA, Func, Priorty, Loctn, 2366,false,false,86,1,0,0,0,0,0,false,1,100,445 AobjTable numObj, GrafLink, VidLink, label, DisOp, PolSen, TarPts, Tgls, TimOc, Tim1, ExDly, Tim2, LenAcc, Func, Priorty,Loctn, 2381,false,false,131,1,0,0,false,0,0,20,0,4,7,100,446 </code> tbodine Link to comment Share on other sites More sharing options...
PsaltyDS Posted February 2, 2010 Share Posted February 2, 2010 (edited) Code tags on this forum should be in square brackets []. P.S. For the second output version, reference the child node by index, because there is more than one with the same name (AobjTable) so you get the first instance every time: $kids = _XMLGetChildNodes($path & "/*[" & $i & "]") P.P.S. For the third output version, use _XMLGetValue(): For $i = 1 To UBound($children) - 1 ConsoleWrite($children[$i] & @CRLF) $kids = _XMLGetChildNodes($path & "/*[" & $i & "]") $sNodes = "" $aValues = "" $sValues = "" For $j = 1 To UBound($kids) - 1 $aValues = "" $sValue = "" $sNodes &= $kids[$j] & "," $aValues = _XMLGetValue($path & "/*[" & $i & "]/" & $kids[$j]) If IsArray($aValues) Then $sValue = $aValues[1] $sValues &= $sValue & "," Next ConsoleWrite($sNodes & @CRLF & _ $sValues & @CRLF) Next Edited February 2, 2010 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...
tbodine88 Posted February 3, 2010 Author Share Posted February 3, 2010 thanks so much. I wrote a parser all in autoit, but it runs so slow I thought I'd try this. The difference is amazing. tbodine Link to comment Share on other sites More sharing options...
JonF Posted April 26, 2013 Share Posted April 26, 2013 (edited) I'm no XML expert. I'm trying to parse an XML file that looks like: <xml xmlns:rs='urn:schemas-microsoft-com:rowset' xmlns:z='#RowsetSchema'> <rs:data> <z:row description='' entry='*@*.*.aa.com' type='1' /> <z:row description='' entry='*@*.aa.com' type='1' /> <z:row description='' entry='*@*.advanstar.com' type='1' /> . . . I'm trying to use the code above, but I can't get by "path=". I tried "/rs:data" but didn't like the namespace. I've tried all sorts of things to put in the namespace argument of _XMLFileOpen, and nothing works. Help? Edited April 26, 2013 by JonF 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