homerjsimpson
03-24-2007, 08:15 PM
how do i check if a node exists in an xml tree??
im using php5 dom functions.
thanks
im using php5 dom functions.
thanks
|
Click to See Complete Forum and Search --> : how to check if XML node exists?? homerjsimpson 03-24-2007, 08:15 PM how do i check if a node exists in an xml tree?? im using php5 dom functions. thanks Weedpacket 03-25-2007, 01:28 AM Use an XPath query to specify which node you want to locate. If the list returned is empty, the node isn't in the document. homerjsimpson 03-25-2007, 05:46 AM This is the xml file I have: <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Header> <ebl:RequesterCredentials xmlns="urn:ebay:apis:eBLBaseComponents" xmlns:ebl="urn:ebay:apis:eBLBaseComponents" soapenv:mustUnderstand="0"> <ebl:NotificationSignature xmlns:ebl="urn:ebay:apis:eBLBaseComponents">/9zSjcXsC6VqZ8XkDzYu/ZA==</ebl:NotificationSignature> </ebl:RequesterCredentials> </soapenv:Header> <soapenv:Body> <GetItemTransactionsResponse xmlns="urn:ebay:apis:eBLBaseComponents"> <Timestamp>2007-03-25T08:06:30.254Z</Timestamp> <Ack>Success</Ack> <CorrelationID>171816430</CorrelationID> <Version>503</Version> <Build>e503_core_Bundled_4329350_R1</Build> <NotificationEventName>EndOfAuction</NotificationEventName> <RecipientUserID>digitalsales1</RecipientUserID> <PaginationResult> <TotalNumberOfPages>1</TotalNumberOfPages> <TotalNumberOfEntries>1</TotalNumberOfEntries> </PaginationResult> <HasMoreTransactions>false</HasMoreTransactions> <TransactionsPerPage>100</TransactionsPerPage> <PageNumber>1</PageNumber> <ReturnedTransactionCountActual>1</ReturnedTransactionCountActual> <Item> <AutoPay>false</AutoPay> <BuyerProtection>ItemIneligible</BuyerProtection> <BuyItNowPrice currencyID="USD">33.0</BuyItNowPrice> <Currency>USD</Currency> <ItemID>110017876641</ItemID> <ListingDetails> <StartTime>2007-03-23T14:00:17.000Z</StartTime> <EndTime>2007-03-25T08:06:25.000Z</EndTime> <ExpressListing>false</ExpressListing> </ListingDetails> <ListingType>Chinese</ListingType> <PaymentMethods>PayPal</PaymentMethods> <PrimaryCategory> <CategoryID>29792</CategoryID> </PrimaryCategory> <PrivateListing>false</PrivateListing> <Quantity>1</Quantity> <SecondaryCategory> <CategoryID>0</CategoryID> </SecondaryCategory> <Seller> <AboutMePage>false</AboutMePage> <EIASToken>nY+sHZ2PrBmdj6wVnY+sEZ2PdrA2dj6wFk4CnCpSGqQqdj6x9nY+seQ==</EIASToken> <Email>digitalsales@sales.com</Email> <FeedbackScore>10</FeedbackScore> <FeedbackPrivate>false</FeedbackPrivate> <FeedbackRatingStar>Yellow</FeedbackRatingStar> <IDVerified>true</IDVerified> <eBayGoodStanding>true</eBayGoodStanding> <NewUser>false</NewUser> <RegistrationDate>1995-01-01T19:59:59.000Z</RegistrationDate> <Site>US</Site> <Status>Confirmed</Status> <UserID>digitalsales1</UserID> <UserIDChanged>true</UserIDChanged> <UserIDLastChanged>2007-03-19T20:59:29.000Z</UserIDLastChanged> <VATStatus>NoVATTax</VATStatus> <SellerInfo> <AllowPaymentEdit>true</AllowPaymentEdit> <CheckoutEnabled>true</CheckoutEnabled> <CIPBankAccountStored>false</CIPBankAccountStored> <GoodStanding>true</GoodStanding> <LiveAuctionAuthorized>false</LiveAuctionAuthorized> <MerchandizingPref>OptIn</MerchandizingPref> <QualifiesForB2BVAT>false</QualifiesForB2BVAT> <SellerLevel>None</SellerLevel> <StoreOwner>false</StoreOwner> <ExpressEligible>false</ExpressEligible> <ExpressWallet>false</ExpressWallet> <SafePaymentExempt>false</SafePaymentExempt> </SellerInfo> </Seller> <SellingStatus> <BidCount>1</BidCount> <ConvertedCurrentPrice currencyID="USD">33.0</ConvertedCurrentPrice> <CurrentPrice currencyID="USD">33.0</CurrentPrice> <QuantitySold>1</QuantitySold> <ListingStatus>Completed</ListingStatus> </SellingStatus> <Site>US</Site> <StartPrice currencyID="USD">32.0</StartPrice> <Title>testing</Title> <GetItFast>false</GetItFast> </Item> <TransactionArray> <Transaction> <AmountPaid currencyID="USD">37.95</AmountPaid> <AdjustmentAmount currencyID="USD">0.0</AdjustmentAmount> <ConvertedAdjustmentAmount currencyID="USD">0.0</ConvertedAdjustmentAmount> <Buyer> <AboutMePage>false</AboutMePage> <EIASToken>nY+sHZ2PrBmdjd6wVnY+sEZ2PrA2dj6wFk4CnCpSLoQ6dj6x9nY+seQ==</EIASToken> <Email>liquitrade@gmail.com</Email> <FeedbackScore>0</FeedbackScore> <FeedbackPrivate>false</FeedbackPrivate> <FeedbackRatingStar>None</FeedbackRatingStar> <IDVerified>false</IDVerified> <eBayGoodStanding>true</eBayGoodStanding> <NewUser>true</NewUser> <RegistrationDate>2007-03-21T01:52:47.000Z</RegistrationDate> <Site>US</Site> <Status>Confirmed</Status> <UserID>homerjsimpson</UserID> <UserIDChanged>false</UserIDChanged> <UserIDLastChanged>2007-03-21T01:52:46.000Z</UserIDLastChanged> <VATStatus>NoVATTax</VATStatus> <BuyerInfo> <ShippingAddress> <Name>homerj simpson</Name> <Street1>street</Street1> <Street2>pobox</Street2> <CityName>pittsburgh</CityName> <StateOrProvince>PA</StateOrProvince> <Country>US</Country> <CountryName>United States</CountryName> <Phone>(412) 778-7878</Phone> <PostalCode>15235</PostalCode> <AddressID>3467918</AddressID> <AddressOwner>eBay</AddressOwner> </ShippingAddress> </BuyerInfo> <UserAnonymized>false</UserAnonymized> </Buyer> <ShippingDetails> <ChangePaymentInstructions>true</ChangePaymentInstructions> <InsuranceFee currencyID="USD">0.0</InsuranceFee> <InsuranceOption>NotOffered</InsuranceOption> <InsuranceWanted>false</InsuranceWanted> <PaymentEdited>false</PaymentEdited> <SalesTax> <SalesTaxPercent>0.0</SalesTaxPercent> <ShippingIncludedInTax>false</ShippingIncludedInTax> </SalesTax> <ShippingServiceOptions> <ShippingService>USPSFirstClass</ShippingService> <ShippingServiceCost currencyID="USD">4.95</ShippingServiceCost> <ShippingServicePriority>1</ShippingServicePriority> <ExpeditedService>false</ExpeditedService> <ShippingTimeMin>2</ShippingTimeMin> <ShippingTimeMax>5</ShippingTimeMax> </ShippingServiceOptions> <InternationalShippingServiceOption> <ShippingService>USPSAirmailParcel</ShippingService> <ShippingServiceCost>6.95</ShippingServiceCost> <ShippingServicePriority>1</ShippingServicePriority> <ShipToLocation>Worldwide</ShipToLocation> </InternationalShippingServiceOption> <ShippingType>Flat</ShippingType> <SellingManagerSalesRecordNumber>282</SellingManagerSalesRecordNumber> <ThirdPartyCheckout>false</ThirdPartyCheckout> <TaxTable/> <GetItFast>false</GetItFast> </ShippingDetails> <ConvertedAmountPaid currencyID="USD">37.95</ConvertedAmountPaid> <ConvertedTransactionPrice currencyID="USD">33.0</ConvertedTransactionPrice> <CreatedDate>2007-03-25T08:06:25.000Z</CreatedDate> <DepositType>None</DepositType> <QuantityPurchased>1</QuantityPurchased> <Status> <eBayPaymentStatus>NoPaymentFailure</eBayPaymentStatus> <CheckoutStatus>CheckoutIncomplete</CheckoutStatus> <LastTimeModified>2007-03-25T08:06:25.000Z</LastTimeModified> <PaymentMethodUsed>None</PaymentMethodUsed> <CompleteStatus>Incomplete</CompleteStatus> <BuyerSelectedShipping>false</BuyerSelectedShipping> </Status> <TransactionID>0</TransactionID> <TransactionPrice currencyID="USD">33.0</TransactionPrice> <BestOfferSale>false</BestOfferSale> <ShippingServiceSelected> <ShippingInsuranceCost currencyID="USD">0.0</ShippingInsuranceCost> <ShippingService>USPSFirstClass</ShippingService> <ShippingServiceCost currencyID="USD">4.95</ShippingServiceCost> </ShippingServiceSelected> <TransactionPlatform>eBay</TransactionPlatform> </Transaction> </TransactionArray> </GetItemTransactionsResponse> </soapenv:Body> </soapenv:Envelope> That is the xml information I get from ebay. I need to access the "ShippingServices" node. $xml = simplexml_load_file("test.xml"); $result = $xml->xpath("//ShippingServices"); print_r($result); It only returns an empty array and it returns an empty array with any node I try to query. Array ( ) I then stumbled upon what seems to be a solution but with problems. I changed "xmlns=" to "xmlns:ns=" in the following line: <GetItemTransactionsResponse xmlns="urn:ebay:apis:eBLBaseComponents"> and the query for ShippingServices still returned an empty string. However When I tried "//RegistrationDate" it worked! So to summarize: With the original xml file I could not retrieve any nodes but after I added ":ns" to that line only some queries work. The queries return an empty array when I use no forward slashes "Shippingservices" for any node name as well, regardless of whether I add ns: to that line. What is going on? I am in need of urgent help. Thank you so much. UPDATE ===== Actually all queries seem to work only after I add ":ns=" to that line. I also tried inserting the namespace decleration in the query itself without modifiying the root elements namespace decleration and it returns the following error: This is what I tried $xml = simplexml_load_file("test.xml"); $result = $xml->xpath("//ns:GetItemTransactionsResponse"); print_r($result); resulted in the following error: Undefined namespace prefix xpath What's going on?? Thanks homerjsimpson 03-25-2007, 07:08 AM ok It seems like the default namespace in the original xml document needs a namespace decleration. I declared my own namespace with the following: $xml->registerXPathNamespace("s", "urn:ebay:apis:eBLBaseComponents"); The queries work now. will I encounter any problems by doing this? thanks Shrike 03-25-2007, 05:48 PM You were looking for a node called ShippingServices, I think it's singular i.e. ShippingService. Maybe just a typo on this thread though ;) homerjsimpson 03-25-2007, 10:27 PM eeeek thanks:rolleyes: ;) bradgrafelman 03-29-2007, 11:39 PM Don't forget to mark this thread resolved (if it is). PHP Builder
Copyright Internet.com Inc. All Rights Reserved. |