如何使用nokogiri从xml文件中提取元素值

时间:2014-09-15 01:56:26

标签: ruby xml parsing nokogiri

从下面的xml文件中,有多个'SellerAccount'元素,我需要提取&存储'SellerAccount'的所有'ServiceCode'值= 98765432。有人可以建议/指导我吗?

    <SellerRequest>
    <Header>
            <ExtractDateTime>2014-09-11T12:04:50</ExtractDateTime>
            <SequenceNumber>1001</SequenceNumber>
            <RecordCount>32</RecordCount>
        </Header>
        <Requests>
    <Request type="Install">
        <ActionCode>101</ActionCode>
            <SellerID>10</SellerID>
            <SellerAccount>123456</SellerAccount>
            <Salutation>Mr</Salutation>
            <FirstName>Matt</FirstName>
            <LastName>Smith</LastName>
            <HomePhoneNumber>91234568</HomePhoneNumber>
            <WorkPhoneNumber></WorkPhoneNumber>
            <MobileNumber>21123456</MobileNumber>
            <Email>xml_file@example.test</Email>
            <StreetNumber>3</StreetNumber>
            <UnitFlatNumber></UnitFlatNumber>
            <StreetNumberSuffix></StreetNumberSuffix>
            <StreetName>Testing DRIVE</StreetName>
            <Suburb>SUBURB</Suburb>
            <City>CITY</City>
            <PostCode>12345</PostCode>
            <SellerUserID>seller</SellerUserID>
            <SellerOrderNumber></SellerOrderNumber>
            <Comment></Comment>

        <NominatedPINNumber>1234</NominatedPINNumber>
        <NextOfKinName>contact</NextOfKinName>
                <NextOfKinAddressLine1>1 testing Drive</NextOfKinAddressLine1>
                <NextOfKinAddressLine2>City</NextOfKinAddressLine2>
                <NextOfKinPhoneNumber>091234567</NextOfKinPhoneNumber>
                <DateOfBirth>1980-01-01</DateOfBirth>
        <NameChangeRequest>N</NameChangeRequest>
        <BillingAddressLine1>3    TESTING DRIVE</BillingAddressLine1>
                <BillingAddressLine2>SUBURB</BillingAddressLine2>
                <BillingAddressLine3>CITY 12345</BillingAddressLine3>
        <PreferredDate>2014-09-12</PreferredDate>
                <PreferredTimeSlot>20</PreferredTimeSlot>
        <BillCycle>1</BillCycle>
<Occurrences>
<Occurrence ID="1">
<ServiceCode>001</ServiceCode>
<ServiceCode>002</ServiceCode>
<ServiceCode>003</ServiceCode>
<ServiceCode>004</ServiceCode>
<ServiceCode>005</ServiceCode>
<ServiceCode>006</ServiceCode>
<ServiceCode>007</ServiceCode>
<ServiceCode>008</ServiceCode>
<ServiceCode>009</ServiceCode>
<ServiceCode>010</ServiceCode>
<ServiceCode>011</ServiceCode>
<ServiceCode>012</ServiceCode>
<ServiceCode>013</ServiceCode>
<ServiceCode>014</ServiceCode>
<ServiceCode>015</ServiceCode>
<ServiceCode>016</ServiceCode>
<ServiceCode>017</ServiceCode>
<ServiceCode>018</ServiceCode>
<ServiceCode>019</ServiceCode>
<ServiceCode>020</ServiceCode>
<ServiceCode>021</ServiceCode>
<ServiceCode>AB123</ServiceCode>
</Occurrence>
<Occurrence ID="2">
<ServiceCode>001</ServiceCode>
<ServiceCode>002</ServiceCode>
<ServiceCode>003</ServiceCode>
<ServiceCode>004</ServiceCode>
<ServiceCode>005</ServiceCode>
<ServiceCode>006</ServiceCode>
<ServiceCode>007</ServiceCode>
<ServiceCode>008</ServiceCode>
<ServiceCode>009</ServiceCode>
<ServiceCode>010</ServiceCode>
<ServiceCode>011</ServiceCode>
<ServiceCode>012</ServiceCode>
<ServiceCode>013</ServiceCode>
<ServiceCode>014</ServiceCode>
<ServiceCode>015</ServiceCode>
<ServiceCode>016</ServiceCode>
<ServiceCode>017</ServiceCode>
<ServiceCode>018</ServiceCode>
<ServiceCode>AB123</ServiceCode>
</Occurrence>
</Occurrences>

</Request>
<Request type="Install">
        <ActionCode>101</ActionCode>
            <SellerID>10</SellerID>
            <SellerAccount>98765432</SellerAccount>
            <Salutation>Mr</Salutation>
            <FirstName>James</FirstName>
            <LastName>Tester</LastName>
            <HomePhoneNumber>91234567</HomePhoneNumber>
            <WorkPhoneNumber>91234568</WorkPhoneNumber>
            <MobileNumber>21123456</MobileNumber>
            <Email>example@email.test</Email>
            <StreetNumber>3</StreetNumber>
            <UnitFlatNumber></UnitFlatNumber>
            <StreetNumberSuffix></StreetNumberSuffix>
            <StreetName>TESTING DRIVE</StreetName>
            <Suburb>SUBURB</Suburb>
            <City>CITY</City>
            <PostCode>12345</PostCode>
            <SellerUserID>seller_2</SellerUserID>
            <SellerOrderNumber></SellerOrderNumber>
            <Comment>new customer</Comment>

        <NominatedPINNumber>3456</NominatedPINNumber>
        <NextOfKinName>contact</NextOfKinName>
                <NextOfKinAddressLine1>1 testing DRIVE</NextOfKinAddressLine1>
                <NextOfKinAddressLine2>City</NextOfKinAddressLine2>
                <NextOfKinPhoneNumber>099876543</NextOfKinPhoneNumber>
                <DateOfBirth>1980-01-01</DateOfBirth>
        <NameChangeRequest>N</NameChangeRequest>
        <BillingAddressLine1>3 TESTING DRIVE</BillingAddressLine1>
                <BillingAddressLine2>SUBURB</BillingAddressLine2>
                <BillingAddressLine3>CITY 12345</BillingAddressLine3>
        <PreferredDate>2014-09-12</PreferredDate>
                <PreferredTimeSlot>20</PreferredTimeSlot>
        <BillCycle>1</BillCycle>
<Occurrences>
<Occurrence ID="1">
<ServiceCode>001</ServiceCode>
<ServiceCode>002</ServiceCode>
<ServiceCode>003</ServiceCode>
<ServiceCode>004</ServiceCode>
<ServiceCode>005</ServiceCode>
<ServiceCode>006</ServiceCode>
<ServiceCode>007</ServiceCode>
<ServiceCode>008</ServiceCode>
<ServiceCode>009</ServiceCode>
<ServiceCode>010</ServiceCode>
<ServiceCode>011</ServiceCode>
<ServiceCode>012</ServiceCode>
<ServiceCode>013</ServiceCode>
<ServiceCode>014</ServiceCode>
<ServiceCode>015</ServiceCode>
<ServiceCode>016</ServiceCode>
<ServiceCode>017</ServiceCode>
<ServiceCode>018</ServiceCode>
<ServiceCode>019</ServiceCode>
<ServiceCode>020</ServiceCode>
<ServiceCode>AB123</ServiceCode>
</Occurrence>
</Occurrences>

</Request>
    </Requests>
</SellerRequest>

1 个答案:

答案 0 :(得分:2)

假设您已经解析了XML:

doc = Nokogiri.XML(xml)

你可以使用XPath来获得你想要的东西:

codes = doc.xpath("//Request[SellerAccount='98765432']//ServiceCode").map(&:text)

text方法只是从Nokogiri对象中提取字符串。

输出:

["001",
 "002",
 "003",
 "004",
 "005",
 "006",
 "007",
 "008",
 "009",
 "010",
 "011",
 "012",
 "013",
 "014",
 "015",
 "016",
 "017",
 "018",
 "019",
 "020",
 "AB123"]