Trying to extract number from XML

sachin86

New Member
Joined
Jul 17, 2011
Messages
3
Hello,

Thank you so much for your time and help! I am trying to extract tax data from a large XML that contains several things. Here is an example of a piece of data:

<Order>
<OrderID>105-8920931-7716261</OrderID>
<PurchaseDate>2011-05-01T04:39:41+00:00</PurchaseDate>
<LastUpdatedDate>2011-05-01T11:38:02+00:00</LastUpdatedDate>
<OrderStatus>Shipped</OrderStatus>
<SalesChannel>merchant</SalesChannel>
<FulfillmentData>
<FulfillmentChannel>Merchant</FulfillmentChannel>
<ShipServiceLevel>Standard</ShipServiceLevel>
<Address>
<City>SHERMAN</City>
<State>TX</State>
<PostalCode>75090-3488</PostalCode>
<Country>US</Country>
</Address>
</FulfillmentData>
<OrderItem>
<ASIN>B0022R74BC</ASIN>
<SKU>MN-UPDC-Q1H3</SKU>
<ItemStatus>Shipped</ItemStatus>
<ProductName>Merkur Futur Double Edge Safety Razor #700 + 5 Free DE Razor Blades</ProductName>
<Quantity>2</Quantity>
<ItemPrice>
<Component>
<Type>Principal</Type>
<Amount currency="USD">140.0</Amount>
</Component>
<Component>
<Type>Tax</Type>
<Amount currency="USD">8.76</Amount>
</Component>
<Component>
<Type>Shipping</Type>
<Amount currency="USD">23.99</Amount>
</Component>
<Component>
<Type>ShippingTax</Type>
<Amount currency="USD">1.5</Amount>
</Component>
</ItemPrice>
</OrderItem>
</Order>


I have a list of thousands of these in one single column (one after the other). What I need to extract is the "Tax" amount collected. In this case, Tax is represented by:

<Component>
<Type>Tax</Type>
<Amount currency="USD">8.76</Amount>
</Component>
<Component>



What I want is an easy way to extract the 8.76 and perhaps put it in a separate column or some way in which I can add all of the taxes collected in an XML file that contains thousands of these individual transactions.

Your help would be much appreciated! Thanks so much for a great forum!!
 
Last edited:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
sachin86,

Welcome to the MrExcel forum.

What version of Excel are you using?

You will generally get much more help (and faster) in this forum if you can post your small samples (what you have and what you expect to achieve) directly in the forum.

To attach screenshots, see below in my Signature block: Post a screen shot with one of these:

If you are not able to give us screenshots, see below in my Signature block: You can upload your workbook to Box Net, and provide us with a link to your workbook.
 
Upvote 0
sachin86,


Sample raw data in worksheet Sheet1 before the macro:


Excel Workbook
ABC
1105-8920931-7716261
22011-05-01T04:39:41+00:00
32011-05-01T11:38:02+00:00
4Shipped
5merchant
6
7Merchant
8Standard
9
10SHERMAN
11TX
1275090-3488
13US
14
15
16
17B0022R74BC
18MN-UPDC-Q1H3
19Shipped
20Merkur Futur Double Edge Safety Razor #700 + 5 Free DE Razor Blades
212
22
23
24Principal
25140
26
27
28Tax
298.76
30
31
32Shipping
3323.99
34
35
36ShippingTax
371.5
38
Sheet1





After the macro:


Excel Workbook
ABC
1105-8920931-7716261Tax
22011-05-01T04:39:41+00:008.76
32011-05-01T11:38:02+00:00
4Shipped
5merchant
6
7Merchant
8Standard
9
10SHERMAN
11TX
1275090-3488
13US
14
15
16
17B0022R74BC
18MN-UPDC-Q1H3
19Shipped
20Merkur Futur Double Edge Safety Razor #700 + 5 Free DE Razor Blades
212
22
23
24Principal
25140
26
27
28Tax
298.76
30
31
32Shipping
3323.99
34
35
36ShippingTax
371.5
38
Sheet1





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub GetTax()
' hiker95, 07/17/2011
' http://www.mrexcel.com/forum/showthread.php?p=2792897
Dim c As Range, firstaddress As String, NR As Long
Application.ScreenUpdating = False
Range("C1") = "Tax"
With Columns(1)
  Set c = .Find("Tax", LookIn:=xlValues, LookAt:=xlWhole)
  If Not c Is Nothing Then
    firstaddress = c.Address
    Do
      If IsNumeric(c.Offset(1)) Then
        NR = Range("C" & Rows.Count).End(xlUp).Offset(1).Row
        Range("C" & NR).Value = c.Offset(1).Value
      End If
      Set c = .FindNext(c)
    Loop While Not c Is Nothing And c.Address <> firstaddress
  End If
End With
Columns(3).AutoFit
Application.ScreenUpdating = True
End Sub


Then run the GetTax macro.
 
Upvote 0
As long as the tax amount is always listed immediately under the word "Tax", I think this formula will get the sum for you...

=SUMIF(A1:A10000,"Tax",A2:A10001)

Note that the two specified ranges are the same size and offset one row from each other.... change the top end of the ranges so they will cover all of your data.
 
Upvote 0
Hello!

Thank you all so much for your help and effort! I really appreciate it! Unfortunately, it looks like because I did not know how to paste my code into the forum, the macros and other suggestions did not work. I have downloaded the Jeanie tool so I can paste properly. Here is the actual code I need to extract the tax number from (again, in this example it is $8.76).


Excel Workbook
A
439
440
441*105-8920931-7716261
442*2011-05-01T04:39:41+00:00
443*2011-05-01T11:38:02+00:00
444*Shipped
445*Merchant
446*
447* *Merchant
448* *Standard
449* *
450* * *SHERMAN
451* * *TX
452* * *75090-3488
453* * *US
454* *
455*
456*
457* *B0022R74BC
458* *MN-UPDC-Q1H3
459* *Shipped
460* *Merkur Futur Double Edge Safety Razor #700 + 5 Free DE Razor Blades
461* *2
462* *
463* * *
464* * * *Principal
465* * * *140.0
466* * *
467* * *
468* * * *Tax
469* * * *8.76
470* * *
471* * *
472* * * *Shipping
473* * * *23.99
474* * *
475* * *
476* * * *ShippingTax
477* * * *1.5
478* * *
479* *
480*
481
482
4282070483



Now, please note that I need to calculate both the TAX and the SHIPPINGTAX amounts. In this case, that is 8.76 for TAX and 1.50 for SHIPPINGTAX.

Can you please help now that I have pasted it properly? I am so thankful I found this place!
 
Upvote 0
This may be a good candidate for running a xml query directly on the xml source.
 
Upvote 0
Give this a try,

Excel Workbook
ABC
1Principal**
2140**
3***
4***
5Tax*8.76
68.76**
7***
8***
9Shipping**
1023.99**
11***
12***
13ShippingTax**
141.5**
15***
16* tax*34
1734**
Sheet8
 
Upvote 0
If you want Tax source on one column and Tax amount adajcent to it then try this :
Excel Workbook
ABCD
1Principal***
2140***
3****
4****
5Tax*Tax8.76
68.76***
7****
8****
9Shipping***
1023.99***
11****
12****
13ShippingTax*ShippingTax1.5
Sheet8
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,723
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top