Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 17th, 2004, 01:00 AM
Registered User
 
Join Date: Mar 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using XPath to get a certain field from XML (UPS)

First, I apologize for whats probably a newbie question. I've spent the last 2 days scrounging the web trying to find an answer that makes sense to me and I keep seeing references to XPath and querying an XML file, so I feel I'm CLOSE, I'm just not getting how to tell it WHICH field I wanna select and how to tell it what to call that value when I need to use it in a SQL statement. I have been able to write an XSL file to display this information through an ASP page but now I need to take the shipping charge, and write it to my database for that order (add it to the subtotal, etc - NOT a problem, once I have it as a variable I can use). So lets start with my XML file (returned from UPS):

===========================================
XML File
===========================================

<?xml version="1.0"?>
<RatingServiceSelectionResponse>
    <Response>
        <TransactionReference>
            <CustomerContext>Rating and Service</CustomerContext>
            <XpciVersion>1.0001</XpciVersion>
        </TransactionReference>
        <ResponseStatusCode>1</ResponseStatusCode>
        <ResponseStatusDescription>Success</ResponseStatusDescription>
    </Response>
    <RatedShipment>
        <Service>
            <Code>03</Code>
        </Service>
        <BillingWeight>
            <UnitOfMeasurement>
                <Code>LBS</Code>
            </UnitOfMeasurement>
            <Weight>2.0</Weight>
        </BillingWeight>
        <TransportationCharges>
            <CurrencyCode>USD</CurrencyCode>
            <MonetaryValue>5.96</MonetaryValue>
        </TransportationCharges>
        <ServiceOptionsCharges>
            <CurrencyCode>USD</CurrencyCode>
            <MonetaryValue>0.00</MonetaryValue>
        </ServiceOptionsCharges>
        <TotalCharges>
            <CurrencyCode>USD</CurrencyCode>
            <MonetaryValue>5.96</MonetaryValue>
        </TotalCharges>
        <GuaranteedDaysToDelivery></GuaranteedDaysToDelivery>
        <ScheduledDeliveryTime></ScheduledDeliveryTime>
        <RatedPackage>
            <TransportationCharges>
                <CurrencyCode>USD</CurrencyCode>
                <MonetaryValue>5.96</MonetaryValue>
            </TransportationCharges>
            <ServiceOptionsCharges>
                <CurrencyCode>USD</CurrencyCode>
                <MonetaryValue>1.00</MonetaryValue>
            </ServiceOptionsCharges>
            <TotalCharges>
                <CurrencyCode>USD</CurrencyCode>
                <MonetaryValue>6.96</MonetaryValue>
            </TotalCharges>
            <Weight>1.3</Weight>
            <BillingWeight>
                <UnitOfMeasurement>
                    <Code>LBS</Code>
                </UnitOfMeasurement>
                <Weight>2.0</Weight>
            </BillingWeight>
        </RatedPackage>
    </RatedShipment>
</RatingServiceSelectionResponse>

===========================================
ASP Snippet (where I read in the XML and XSL files and display
===========================================

  set xmlDoc=server.createObject("Msxml2.FreeThreadedDOM Document.4.0")
  set xslDoc=server.createObject("Msxml2.FreeThreadedDOM Document.4.0")
  xmldoc.async = false
  xslDoc.async = false

  xmlDoc.load "e:\inetpub\wwwroot\kitsforcrafts\ups\" & strFileName
  if (xmlDoc.parseError.errorCode <> 0) then
    Response.Write "XML error - " & "<br>"
    Response.Write "Error Reason: " & xmlDoc.parseError.reason & "<br>"
    Response.Write "Source: " & xmlDoc.parseError.srcText & "<br>"
    Response.Write "Error Line: " & xmlDoc.parseError.line & "<br>"
    Response.Write "Error Position: " & xmlDoc.parseError.linepos & "<br>"
  end if

  xslDoc.load "e:\inetpub\wwwroot\kitsforcrafts\shipping.xsl "
  if (xslDoc.parseError.errorCode <> 0) then
    Response.Write "XSL error - " & "<br>"
    Response.Write "Error Reason: " & xslDoc.parseError.reason & "<br>"
    Response.Write "Source: " & xslDoc.parseError.srcText & "<br>"
    Response.Write "Error Line: " & xslDoc.parseError.line & "<br>"
    Response.Write "Error Position: " & xslDoc.parseError.linepos & "<br>"
  end if

  response.write xmlDoc.transformNode(xslDoc)

===========================================

And yes, I know I should be using Server.MapPath but I was just trying to get it WORKING and so hardcoded the paths in (I will go back and change them). The loads DO work, I can display the results just fine. So after that response.write line above, I need to somehow get the field (RatingServiceSelectionResponse/RatedShipment/RatedPackage/TotalCharges/MonetaryValue - the 6.96) into a variable called curShipping that I can use in a SQL statement such as:

"UPDATE CUSTOMER SET SHIPPING_CHARGE = '" & FormatCurrency(curShipping) & "' WHERE ORDER_ID = 78127"

Or

GRAND_TOTAL = SUB_TOTAL + CurShipping

So, what I'm looking for is the code to put in after that line that gets the shipping charge stored in curShipping. XML is already read in as xmlDoc (assuming I can still use it as long as I put the code before the set xmlDoc = nothing line). I probably won't be using XML on a regular basis (at least without another year to figure stuff out), but unfortunately, a windows update a few weeks ago broke the current system we were using to get shipping charges from UPS and its been a real hassle not having that automatic shipping figured when someone checks out through our website (I've got one server, I haven't updated yet that we're running from, but its old and decrepit and I need to get the site moved to the NEW machine but can't until I get the shipping stuff re-written). Too much information I'm sure, but I'm just desperate at this point to get it WORKING.


 
Old March 17th, 2004, 05:00 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Code:
Set nodeCharge = xmlDoc.selectSingleNode("RatingServiceSelectionResponse/RatedShipment/RatedPackage/TotalCharges/MonetaryValue")
If Not nodeCharge Is Nothing Then
    curShipping = nodeCharge.text
End If
hth
Phil
 
Old March 17th, 2004, 05:44 PM
Registered User
 
Join Date: Mar 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you, thank you, thank you, thank you!!!!! Works pefect AND makes sense even (in case I find another field I need to bring in). I hope not to have to bug you guys again, you are wonderful!

 
Old March 18th, 2004, 04:58 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by taleriana
 Thank you, thank you, thank you, thank you!!!!! Works pefect AND makes sense even (in case I find another field I need to bring in). I hope not to have to bug you guys again, you are wonderful!
You're welcome.
You can bug us as much as you like. We always have the option not to answer ;)
 
Old May 24th, 2005, 08:00 AM
Authorized User
 
Join Date: Jun 2003
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default



    I'm NEW to XML and I don't know where to start to get this to work. Can some one help me please. Thanks in advance

Thanks

Sincerely; Oscar Martinez





Similar Threads
Thread Thread Starter Forum Replies Last Post
XML: How to select a node with ' in it using Xpath guozai BOOK: Professional C#, 2nd and 3rd Editions 1 October 6th, 2006 12:37 PM
XPATH Help, Please- Excel XML Workbook jftaylor BOOK: XSLT Programmer's Reference, 2nd Edition 2 September 28th, 2006 08:47 PM
Read XML to DataSet with XPath? striker9 ASP.NET 2.0 Basics 1 July 25th, 2006 03:30 AM
System.Xml.XmlException with XPath ronaldcampfaso C# 1 December 31st, 2005 03:55 AM
UPS servlet interaction with XML via PHP fsockopen karateka Pro PHP 2 October 20th, 2004 12:24 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.