2014-10-01 48 views
0

我會在第4天敲我的頭 - 我曾嘗試的VBScript,JavaScript和jQuery的讀取XML使用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>[email protected]</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>[email protected]</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>[email protected]</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>[email protected]</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>[email protected]</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>[email protected]</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> 
+0

嘗試更改'$(xml).find('OpenShipments')。each(function(){'for'$(xml).find('OpenShipment')。each(function(){' – juvian 2014-10-01 19:50:53

回答

0

隨着問題被標記的VBScript和XML 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:

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