使用jQuery读取XML - 一起运行多个记录

时间:2014-10-01 19:48:36

标签: javascript jquery xml vbscript xml-parsing

我要在第4天敲打我的头 - 我尝试过vbscript,javascript和jQuery

jQuery是我最接近获得可读性的东西(但是,我可以接受任何东西

我正在导入UPS XML文件以用于结束日报告。我需要的只是和

我需要的是能够将结果拉入ASP脚本以更新我的购物车。 XML文件将是本地的,我将把它上传到我的服务器,然后运行脚本。

这是我的HTML,它在一行中产生以下结果:

100513100516 - 1Z12345X0341863059 1Z12345X0342721665

我希望它显示如下:

100513 - 1Z12345X0341863059

100516 - 1Z12345X0342721665

<!DOCTYPE html>
<html>

<head>

<script src="jquery.min.js" type="text/javascript"></script> 

<script type="text/javascript">
$(document).ready(function(){
      $("#dvContent").append("");
      $.ajax({
        type: "GET",
        url: "UPSImportTracking.xml",
        dataType: "xml",
        success: function(xml){
        $(xml).find('OpenShipments').each(function(){
          var cID = $(this).find('CustomerID').text();
          var tID = $(this).find('TrackingNumbers').text();
                $("#dvContent").append("<p>"+cID+" - "+tID+"</p>");
        });
      },
      error: function() {
        alert("An error occurred while processing XML file.");
      }
    });
});
</script>
</head>

<body>

<div id="dvContent">
</div>
</body>

</html>

这是UPS XML

<?xml version="1.0" encoding="windows-1252"?>
<OpenShipments xmlns="x-schema:OpenShipments.xdr">
    <OpenShipment xmlns="x-schema: OpenShipments.xdr" ProcessStatus="Processed" ShipmentOption="SC">
        <ShipTo>
            <CustomerID>100513</CustomerID>
            <CompanyOrName>MACHXX</CompanyOrName>
            <Attention>Robert Brady </Attention>
            <Address1>902 Old Coupland Rd</Address1>
            <Address2></Address2>
            <CityOrTown>Taylor</CityOrTown>
            <PostalCode>76574</PostalCode>
            <StateProvinceCounty>TX</StateProvinceCounty>
            <CountryTerritory>United States</CountryTerritory>
            <Telephone>512-888-3900</Telephone>
            <EmailAddress>jcdisciple@gmail.com</EmailAddress>
            <ResidentialIndicator>1</ResidentialIndicator>
        </ShipTo>
        <ShipmentInformation>
            <ServiceType>GND</ServiceType>
            <NumberOfPackages>1</NumberOfPackages>
            <DescriptionOfGoods>Industrial Metals</DescriptionOfGoods>
            <ShipperNumber>12345X</ShipperNumber>
            <BillingOption>PP</BillingOption>
            <QVNOption>
                <QVNRecipientAndNotificationTypes>
                    <EMailAddress>jcdisciple@gmail.com</EMailAddress>
                    <Ship>1</Ship>
                </QVNRecipientAndNotificationTypes>
                <SubjectLine>Your Order is being Prepared for Shipment.</SubjectLine>
                <Memo></Memo>
            </QVNOption>
        </ShipmentInformation>
        <Package>
            <PackageType>CP</PackageType>
            <Weight>1</Weight>
            <LargePackageIndicator></LargePackageIndicator>
            <Reference1>100513</Reference1>
            <Reference2></Reference2>
        </Package>
        <ReturnTo>
            <CompanyName>Metals4U-Dallas</CompanyName>
            <ContactPerson></ContactPerson>
            <AddressLine1>1240 Majesty Rd</AddressLine1>
            <AddressLine2></AddressLine2>
            <AddressLine3></AddressLine3>
            <City>Dallas</City>
            <CountryCode>United States</CountryCode>
            <PostalCode>75247</PostalCode>
            <StateOrProvince>TX</StateOrProvince>
            <Residential></Residential>
            <EmailAddress1>robert.brady@metals4u.com</EmailAddress1>
        </ReturnTo>
        <ProcessMessage>

            <ShipmentRates>
                <ShipmentCharges>
                    <Rate>
                        <Published>12.83</Published>
                        <Negotiated>0.00</Negotiated>
                    </Rate>
                </ShipmentCharges>
                <ShipperCharges>
                    <Rate>
                        <Published>12.83</Published>
                        <Negotiated>0.00</Negotiated>
                    </Rate>
                </ShipperCharges>
                <ReceiverCharges>
                    <Rate>
                        <Published>0.00</Published>
                        <Negotiated>0.00</Negotiated>
                    </Rate>
                </ReceiverCharges>
                <QVN>
                    <Rate>
                        <Published>0.00</Published>
                        <Negotiated>0.00</Negotiated>
                    </Rate>
                </QVN>
                <ResidentialSurcharge>
                    <Rate>
                        <Published>2.90</Published>
                        <Negotiated></Negotiated>
                    </Rate>
                </ResidentialSurcharge>
                <PackageRates>
                    <PackageRate>
                        <TrackingNumber>1Z12345X0341863059</TrackingNumber>
                        <PackageCharges>
                            <Rate>
                            <Published>12.83</Published>
                            <Negotiated>0.00</Negotiated>
                            </Rate>
                        </PackageCharges>
                        <Delivery_AreaSurcharge>
                            <Rate>
                            <Published>2.85</Published>
                            <Negotiated>0.00</Negotiated>
                            </Rate>
                        </Delivery_AreaSurcharge>
                    </PackageRate>
                </PackageRates>
            </ShipmentRates>
            <TrackingNumbers>
                <TrackingNumber>1Z12345X0341863059</TrackingNumber>
            </TrackingNumbers>
            <ImportID></ImportID>
            <Reference1>100513</Reference1>
            <Reference2></Reference2>
        <ShipmentID></ShipmentID>
        <PRONumber></PRONumber>
        </ProcessMessage>
    </OpenShipment>
    <OpenShipment xmlns="x-schema: OpenShipments.xdr" ProcessStatus="Processed" ShipmentOption="SC">
        <ShipTo>
            <CustomerID>100516</CustomerID>
            <CompanyOrName>C/O: Robert Brady</CompanyOrName>
            <Attention>Robert Brady </Attention>
            <Address1>902 Old Coupland Rd</Address1>
            <Address2></Address2>
            <CityOrTown>Taylor</CityOrTown>
            <PostalCode>76574</PostalCode>
            <StateProvinceCounty>TX</StateProvinceCounty>
            <CountryTerritory>United States</CountryTerritory>
            <Telephone>512-888-3900</Telephone>
            <EmailAddress>jcdisciple@gmail.com</EmailAddress>
            <ResidentialIndicator>1</ResidentialIndicator>
        </ShipTo>
        <ShipmentInformation>
            <ServiceType>GND</ServiceType>
            <NumberOfPackages>1</NumberOfPackages>
            <DescriptionOfGoods>Industrial Metals</DescriptionOfGoods>
            <ShipperNumber>12345X</ShipperNumber>
            <BillingOption>PP</BillingOption>
            <QVNOption>
                <QVNRecipientAndNotificationTypes>
                    <EMailAddress>jcdisciple@gmail.com</EMailAddress>
                    <Ship>1</Ship>
                </QVNRecipientAndNotificationTypes>
                <SubjectLine>Your Order is being Prepared for Shipment.</SubjectLine>
                <Memo></Memo>
            </QVNOption>
        </ShipmentInformation>
        <Package>
            <PackageType>CP</PackageType>
            <Weight>1</Weight>
            <LargePackageIndicator></LargePackageIndicator>
            <Reference1>100516</Reference1>
            <Reference2></Reference2>
        </Package>
        <ReturnTo>
            <CompanyName>Metals4U-Dallas</CompanyName>
            <ContactPerson></ContactPerson>
            <AddressLine1>1240 Majesty Rd</AddressLine1>
            <AddressLine2></AddressLine2>
            <AddressLine3></AddressLine3>
            <City>Dallas</City>
            <CountryCode>United States</CountryCode>
            <PostalCode>75247</PostalCode>
            <StateOrProvince>TX</StateOrProvince>
            <Residential></Residential>
            <EmailAddress1>robert.brady@metals4u.com</EmailAddress1>
        </ReturnTo>
        <ProcessMessage>

            <ShipmentRates>
                <ShipmentCharges>
                    <Rate>
                        <Published>12.83</Published>
                        <Negotiated>0.00</Negotiated>
                    </Rate>
                </ShipmentCharges>
                <ShipperCharges>
                    <Rate>
                        <Published>12.83</Published>
                        <Negotiated>0.00</Negotiated>
                    </Rate>
                </ShipperCharges>
                <ReceiverCharges>
                    <Rate>
                        <Published>0.00</Published>
                        <Negotiated>0.00</Negotiated>
                    </Rate>
                </ReceiverCharges>
                <QVN>
                    <Rate>
                        <Published>0.00</Published>
                        <Negotiated>0.00</Negotiated>
                    </Rate>
                </QVN>
                <ResidentialSurcharge>
                    <Rate>
                        <Published>2.90</Published>
                        <Negotiated></Negotiated>
                    </Rate>
                </ResidentialSurcharge>
                <PackageRates>
                    <PackageRate>
                        <TrackingNumber>1Z12345X0342721665</TrackingNumber>
                        <PackageCharges>
                            <Rate>
                            <Published>12.83</Published>
                            <Negotiated>0.00</Negotiated>
                            </Rate>
                        </PackageCharges>
                        <Delivery_AreaSurcharge>
                            <Rate>
                            <Published>2.85</Published>
                            <Negotiated>0.00</Negotiated>
                            </Rate>
                        </Delivery_AreaSurcharge>
                    </PackageRate>
                </PackageRates>
            </ShipmentRates>
            <TrackingNumbers>
                <TrackingNumber>1Z12345X0342721665</TrackingNumber>
            </TrackingNumbers>
            <ImportID></ImportID>
            <Reference1>100516</Reference1>
            <Reference2></Reference2>
        <ShipmentID></ShipmentID>
        <PRONumber></PRONumber>
        </ProcessMessage>
    </OpenShipment>
</OpenShipments>

1 个答案:

答案 0 :(得分:0)

由于问题标记为VBScript,而Windows的Windows工具是msxml:

Dim oFS      : Set oFS      = CreateObject("Scripting.FileSystemObject")
Dim sFSpec   : sFSpec       = oFS.GetAbsolutePathName("..\testdata\xml\26149392.xml")
Dim objMSXML : Set objMSXML = CreateObject("Msxml2.DOMDocument")
objMSXML.setProperty "SelectionLanguage", "XPath"
objMSXML.async = False
objMSXML.load sFSpec

If 0 = objMSXML.parseError Then
   Dim sXPath   : sXPath       = "/OpenShipments/OpenShipment"
   Dim ndlShipm : Set ndlShipm = objMSXML.selectNodes(sXPath)
   Dim ndShipm
   For Each ndShipm In ndlShipm
       Dim Id : Id = ndShipm.selectSingleNode("ShipTo/CustomerID").text
       Dim ndlTrack : Set ndlTrack = ndShipm.selectNodes("ProcessMessage/TrackingNumbers")
       Dim ndTrack
       For Each ndTrack In ndlTrack
           WScript.Echo Id, ndTrack.text
       Next
   Next
Else
   WScript.Echo objMSXML.parseError.reason
End If

并且在VBScript和JScript中使用COMponents(几乎)相同:

var oFS = new ActiveXObject("Scripting.FileSystemObject");
var sFSpec = oFS.GetAbsolutePathName("..\\testdata\\xml\\26149392.xml");
var objMSXML = new ActiveXObject("Msxml2.DOMDocument");
objMSXML.setProperty("SelectionLanguage", "XPath");
objMSXML.async = false;
objMSXML.load(sFSpec);

if (0 == objMSXML.parseError) {
   var sXPath = "/OpenShipments/OpenShipment";
   var ndlShipm = objMSXML.selectNodes(sXPath);
   for (var i = 0; i < ndlShipm.length; ++i) {
     var ndShipm = ndlShipm(i);
     var Id = ndShipm.selectSingleNode("ShipTo/CustomerID").text;
     var ndlTrack = ndShipm.selectNodes("ProcessMessage/TrackingNumbers")
     for (var j = 0; j < ndlTrack.length; ++j) {
        WScript.Echo(Id, ndlTrack(j).text);
     }
   }
} else {
   WScript.Echo(objMSXML.parseError.reason)
}

两者的输出:

cscript 26149392.js / 26149392.vbs
100513 1Z12345X0341863059
100516 1Z12345X0342721665