Link to home
Start Free TrialLog in
Avatar of curiouswebster
curiouswebsterFlag for United States of America

asked on

Using XPath to read XML data

I need to read an XML file and retrieve a list it contains, then iterate through it.

Can I do this with XPath?

Could someone show me how?

thanks,
newbieweb
Avatar of HooKooDooKu
HooKooDooKu

XPath is a way of selecting (an) xml node(s).  Basically, it's sort of like a query language for an XML file.
http://msdn.microsoft.com/en-us/library/ms256086.aspx

As to how to use it, I've used it using Microsoft's DOM (Document Object Model) of access to an XML file.
http://msdn.microsoft.com/en-us/library/ms753804(VS.85).aspx

When I was using XPath, Microsofts XML reader was at version 4, so the crude code snippet below (written in VB6) is using version 4 (msxml4.dll).
Dim Doc As MSXML2.DOMDocument40
Dim RootNode as MSXML2.IXMLDOMElement 
Dim NodeList As MSXML2.IXMLDOMNodeList
Dim Node As MSXML2.IXMLDOMElement
Dim XPathString as String
 
    Set Doc = New DOMDocument40
    Doc.async = False
    Doc.Load(FileName)
    Call Doc.setProperty("SelectionLanguage", "XPath")    'Allow XPath Queries
 
    Set RootNode = Doc.documentElement
    XPathString = 'Set some query value here
    Set NodeList = RootNode.selectNodes( XPathString )
    Set Node = NodeList.nextNode
    Do Until Node Is Nothing
        'Do stuff with Node object such as 
        'Node.getAttribute( Attribute Name here )
        Set Node = NodeList.nextNode
    Loop

Open in new window

SOLUTION
Avatar of Toms Edison
Toms Edison
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of curiouswebster

ASKER

HooKooDooKu,

Could you do this in C#?

newbieweb
I'm not a C# programmer, but by pulling up a C# editor, it looks like the following code would be functionally equivilent.
using System.Xml    //Add Xml capability to the code
 
...
 
XmlDocument Doc;
Doc.Load( FileName );
XmlElement RootNode = Doc.DocumentElement;
String XPathString = "What ever XPath Query you Need";
XmlNodeList NodeList = RootNode.SelectNodes( XPathString );
for( int i = 0; i < NodeList.Count; i++ )
{
    XmlNode Node = NodeList.Item(i);
    //Do Stuff with Node Here
}

Open in new window

OK.  Now I have the C# code compiling and finding the document okay.  But I'm not sure what to use for XPathString.

I need to get all records where

RecordLocator = "YVRSEA"
and need to extract FName and LName for each Passenger record.

How do i do this?

newbieweb

    private void PopulatePage()
    {
        XmlDocument Doc = new XmlDocument();
        Doc.Load( @"C:\MyWork\Test\BlankASPTest\testdata\reservation.xml");
        XmlElement RootNode = Doc.DocumentElement;
        String XPathString = "What ever XPath Query you Need";
        XmlNodeList NodeList = RootNode.SelectNodes( XPathString );
        for( int i = 0; i < NodeList.Count; i++ )
        {
            XmlNode Node = NodeList.Item(i);
            //Do Stuff with Node Here
        }
    }
What does the XML look like (sort of hard to build a database query without knowing the structure of the database)?
<?xml version="1.0" encoding="UTF-8" ?>


<FindResByLocatorResponse>
        <TransactionID>TEST-031219161726</TransactionID>
        <PNR>
                <RecordLocator>YVRSEA</RecordLocator>
                <Type>0</Type>
                <Header>Regular</Header>
                <Passenger>
                        <LName>METHENY</LName>
                        <FName>PAT</FName>
                        <Type>ADT</Type>
                        <PassengerNumber>1.1</PassengerNumber>
                        <FFN>
                            <Number>098745341</Number>
                            <Type>AS</Type>
                            <AwardLevel>1</AwardLevel>
                        </FFN>
                       <ExistingBagCount>0</ExistingBagCount>
                        <MaxFreeBagCount>2</MaxFreeBagCount>
                        <StartPassenger/>
                        <Seat>
                                <Number>40-C</Number>
                        </Seat>
                        <CouponBook>
                                <FName>PAT</FName>
                                <LName>METHENY</LName>
                                <Coupon>
                                        <Carrier>NW</Carrier>
                                        <FlightNumber>711</FlightNumber>
                                        <DeparturePoint>MCO</DeparturePoint>
                                        <ArrivalPoint>DTW</ArrivalPoint>
                                        <CouponNumber>01</CouponNumber>
                                        <Date>19DEC03</Date>
                                        <DepartureTime>2055</DepartureTime>
                                        <Class>Q</Class>
                                        <FareBasis>QEP21NR1</FareBasis>
                                        <ActualStatus>O</ActualStatus>
                                        <SegmentNumber>01</SegmentNumber>
                                        <Status>OK</Status>
                                        <Valid/>
                                        <DepartureCity>Orlando</DepartureCity>
                                        <ArrivalCity>Detroit</ArrivalCity>
                                </Coupon>
                                <Coupon>
                                        <Carrier>NW</Carrier>
                                        <FlightNumber>778</FlightNumber>
                                        <DeparturePoint>DTW</DeparturePoint>
                                        <ArrivalPoint>MCO</ArrivalPoint>
                                        <CouponNumber>02</CouponNumber>
                                        <Date>30DEC03</Date>
                                        <DepartureTime>0700</DepartureTime>
                                        <Class>K</Class>
                                        <FareBasis>KEO21NR1</FareBasis>
                                        <ActualStatus>O</ActualStatus>
                                        <Status>OK</Status>
                                        <DepartureCity>Detroit</DepartureCity>
                                        <ArrivalCity>Orlando</ArrivalCity>
                                </Coupon>
                                <TicketNumber>0121572614120    </TicketNumber>
                                <DateOfIssue>23SEP03</DateOfIssue>
                                <PlaceOfIssue>NORTHWEST AIRLINES/ORBITZ MPLS/ST PAUL MN </PlaceOfIssue>
                                <FormOfPayment>VI</FormOfPayment>
                                <FOPType>CC</FOPType>
                                <Fare>
                                        <Base>  257.68</Base>
                                        <Total>  290.50</Total>
                                        <Currency>USD</Currency>
                                        <Tax>
                                                <Value>US</Value>
                                                <Type>   19.32</Type>
                                        </Tax>
                                        <Tax>
                                                <Value>ZP</Value>
                                                <Type>    6.00</Type>
                                        </Tax>
                                        <Tax>
                                                <Value>XF</Value>
                                                <Type>    7.50</Type>
                                        </Tax>
                                        <Calculation></Calculation>
                                        <CalculationWithTaxes></CalculationWithTaxes>
                                </Fare>
                                <EndorsementRestrictions>NON-REFUNDABLE//PENALTY FOR CHANGES </EndorsementRestrictions>
                        </CouponBook>
                        <NameFieldStatus></NameFieldStatus>
                </Passenger>
                <Passenger>
                        <LName>SCOFIELD</LName>
                        <FName>JOHN</FName>
                        <Type>ADT</Type>
                        <PassengerNumber>2.1</PassengerNumber>
                        <FFN>
                            <Number>880932850434</Number>
                            <Type>CO</Type>
                            <AwardLevel>1</AwardLevel>
                        </FFN>
                        <ExistingBagCount>0</ExistingBagCount>
                        <MaxFreeBagCount>2</MaxFreeBagCount>
                        <Seat>
                                <Number>40-D</Number>
                        </Seat>
                        <CouponBook>
                                <FName>JOHN</FName>
                                <LName>SCOFIELD</LName>
                                <Coupon>
                                        <Carrier>NW</Carrier>
                                        <FlightNumber>711</FlightNumber>
                                        <DeparturePoint>MCO</DeparturePoint>
                                        <ArrivalPoint>DTW</ArrivalPoint>
                                        <CouponNumber>01</CouponNumber>
                                        <Date>19DEC03</Date>
                                        <DepartureTime>2055</DepartureTime>
                                        <Class>Q</Class>
                                        <FareBasis>QEP21NR1</FareBasis>
                                        <ActualStatus>O</ActualStatus>
                                        <SegmentNumber>01</SegmentNumber>
                                        <Status>OK</Status>
                                        <Valid/>
                                        <DepartureCity>Orlando</DepartureCity>
                                        <ArrivalCity>Detroit</ArrivalCity>
                                </Coupon>
                                <Coupon>
                                        <Carrier>NW</Carrier>
                                        <FlightNumber>778</FlightNumber>
                                        <DeparturePoint>DTW</DeparturePoint>
                                        <ArrivalPoint>MCO</ArrivalPoint>
                                        <CouponNumber>02</CouponNumber>
                                        <Date>30DEC03</Date>
                                        <DepartureTime>0700</DepartureTime>
                                        <Class>K</Class>
                                        <FareBasis>KEO21NR1</FareBasis>
                                        <ActualStatus>O</ActualStatus>
                                        <Status>OK</Status>
                                        <DepartureCity>Detroit</DepartureCity>
                                        <ArrivalCity>Orlando</ArrivalCity>
                                </Coupon>
                                <TicketNumber>0121572614120    </TicketNumber>
                                <DateOfIssue>23SEP03</DateOfIssue>
                                <PlaceOfIssue>NORTHWEST AIRLINES/ORBITZ MPLS/ST PAUL MN </PlaceOfIssue>
                                <FormOfPayment>VI</FormOfPayment>
                                <FOPType>CC</FOPType>
                                <Fare>
                                        <Base>  257.68</Base>
                                        <Total>  290.50</Total>
                                        <Currency>USD</Currency>
                                        <Tax>
                                                <Value>US</Value>
                                                <Type>   19.32</Type>
                                        </Tax>
                                        <Tax>
                                                <Value>ZP</Value>
                                                <Type>    6.00</Type>
                                        </Tax>
                                        <Tax>
                                                <Value>XF</Value>
                                                <Type>    7.50</Type>
                                        </Tax>
                                        <Calculation></Calculation>
                                        <CalculationWithTaxes></CalculationWithTaxes>
                                </Fare>
                                <EndorsementRestrictions>NON-REFUNDABLE//PENALTY FOR CHANGES </EndorsementRestrictions>
                        </CouponBook>
                        <NameFieldStatus></NameFieldStatus>
                </Passenger>
                <Passenger>
                        <LName>COLTRANE</LName>
                        <FName>JOHN</FName>
                        <Type>ADT</Type>
                        <PassengerNumber>2.1</PassengerNumber>
                        <FFN>
                            <Number>338847119</Number>
                            <Type>NW</Type>
                            <AwardLevel>1</AwardLevel>
                        </FFN>
                        <ExistingBagCount>0</ExistingBagCount>
                        <MaxFreeBagCount>2</MaxFreeBagCount>
                        <Seat>
                                <Number>40-E</Number>
                        </Seat>
                        <CouponBook>
                                <FName>JOHN</FName>
                                <LName>COLTRANE</LName>
                                <Coupon>
                                        <Carrier>NW</Carrier>
                                        <FlightNumber>711</FlightNumber>
                                        <DeparturePoint>MCO</DeparturePoint>
                                        <ArrivalPoint>DTW</ArrivalPoint>
                                        <CouponNumber>01</CouponNumber>
                                        <Date>19DEC03</Date>
                                        <DepartureTime>2055</DepartureTime>
                                        <Class>Q</Class>
                                        <FareBasis>QEP21NR1</FareBasis>
                                        <ActualStatus>O</ActualStatus>
                                        <SegmentNumber>01</SegmentNumber>
                                        <Status>OK</Status>
                                        <Valid/>
                                        <DepartureCity>Orlando</DepartureCity>
                                        <ArrivalCity>Detroit</ArrivalCity>
                                </Coupon>
                                <Coupon>
                                        <Carrier>NW</Carrier>
                                        <FlightNumber>778</FlightNumber>
                                        <DeparturePoint>DTW</DeparturePoint>
                                        <ArrivalPoint>MCO</ArrivalPoint>
                                        <CouponNumber>02</CouponNumber>
                                        <Date>30DEC03</Date>
                                        <DepartureTime>0700</DepartureTime>
                                        <Class>K</Class>
                                        <FareBasis>KEO21NR1</FareBasis>
                                        <ActualStatus>O</ActualStatus>
                                        <Status>OK</Status>
                                        <DepartureCity>Detroit</DepartureCity>
                                        <ArrivalCity>Orlando</ArrivalCity>
                                </Coupon>
                                <TicketNumber>0121572614120    </TicketNumber>
                                <DateOfIssue>23SEP03</DateOfIssue>
                                <PlaceOfIssue>NORTHWEST AIRLINES/ORBITZ MPLS/ST PAUL MN </PlaceOfIssue>
                                <FormOfPayment>VI</FormOfPayment>
                                <FOPType>CC</FOPType>
                                <Fare>
                                        <Base>  257.68</Base>
                                        <Total>  290.50</Total>
                                        <Currency>USD</Currency>
                                        <Tax>
                                                <Value>US</Value>
                                                <Type>   19.32</Type>
                                        </Tax>
                                        <Tax>
                                                <Value>ZP</Value>
                                                <Type>    6.00</Type>
                                        </Tax>
                                        <Tax>
                                                <Value>XF</Value>
                                                <Type>    7.50</Type>
                                        </Tax>
                                        <Calculation></Calculation>
                                        <CalculationWithTaxes></CalculationWithTaxes>
                                </Fare>
                                <EndorsementRestrictions>NON-REFUNDABLE//PENALTY FOR CHANGES </EndorsementRestrictions>
                        </CouponBook>
                        <NameFieldStatus></NameFieldStatus>
                </Passenger>
                <Segment>
                        <Type>0</Type>
                        <Status>HK</Status>
                        <FlightNumber>0711</FlightNumber>
                        <DeparturePoint>MCO</DeparturePoint>
                        <ArrivalPoint>DTW</ArrivalPoint>
                        <DepartureDate>20MAY04</DepartureDate>
                        <DepartureTime>1025</DepartureTime>
                        <ArrivalDate>20MAY04</ArrivalDate>
                        <ArrivalTime>1243</ArrivalTime>
                        <Class>Q</Class>
                        <Index>01</Index>
                        <StopoverCity></StopoverCity>
                        <BucketTypes></BucketTypes>
                        <BoardTime>2055</BoardTime>
                        <Carrier>NW</Carrier>
                        <DepartureGate>G14</DepartureGate>
                        <ArrivalGate>D14</ArrivalGate>
                        <EquipmentCode>755</EquipmentCode>
                        <FlightStatus>ON TIME</FlightStatus>
                        <DepartureCity>Orlando</DepartureCity>
                        <ArrivalCity>Atlanta</ArrivalCity>
                        <UpgradeStatus>0</UpgradeStatus>
                        <SelectSeatEligible/>
                        <ScheduleChange/>
                </Segment>
                <SeatsAutoAssigned/>
        </PNR>
        <Result>1</Result>
</FindResByLocatorResponse>
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial