php


getElementsByTagName, Retrieiving Specific nodeValue from nodeValue with more than one Value


My XML file looks like this:
<Charge ID="01010101">
<Type>SHIPPING</Type>
<Description>Shipping</Description>
<Amount>8.9500</Amount>
</Charge>
<Charge ID="10101010">
<Type>TAX</Type>
<Description>Sales Tax</Description>
<Amount>0.0000</Amount>
</Charge>
I can make an array out of this
$getcharge = $doc->getElementsByTagName("Charge");
$array = array();
foreach ($getcharge as $node) {
$array[] = $node;
}
Using print_r I can see the nodeValue is constructed like this
[nodeValue] =>
SHIPPING
Shipping
8.9500
Along the same lines, I can make an array from these nodeValues using
foreach ($doc->getElementsByTagName("Charge") as $values) { {
$result[] = $values->nodeValue;
}
}
Output is
Array
(
[0] =>
SHIPPING
Shipping
8.9500
[1] =>
TAX
Sales Tax
0.0000
)
These child nodes are confusing me because they are not in individual nodeValue's, as seen in the array. They are in fact all one nodeValue.
All I am trying to do is loop over Charge and get the <Type></Type> and <Amount></Amount> into variables.
How do I get the single instance of Type and Amount amidst a nodeValue with multiple attributes?
To get those batches of type, amount, description in each of those charges, you can already use $values as your context, you can also use the method ->getElementsByTagName again. Example:
foreach ($doc->getElementsByTagName("Charge") as $values) {
// $values as each `Charge`
$result[] = array(
'id' => $values->getAttribute('ID'),
'type' => $values->getElementsByTagName('Type')->item(0)->nodeValue,
'description' => $values->getElementsByTagName('Description')->item(0)->nodeValue,
'amount' => $values->getElementsByTagName('Amount')->item(0)->nodeValue,
);
}

Related Links

Changing upload_max_filesize on PHP
php session and path setting
php script Config settings
PHP SOAP client that understands multi-part messages?
php check if domain equals value, then perform action
How can I implement RSS 2 in Code Igniter?
Error with MySQL syntax using REPLACE INTO
how to store chechbox value in php
mysql muliple queries in one statement
PHP Static Variables
php code analysis
PHP Object Validation
How to use cms systems like drupal and so on for crawl only? [closed]
How do I loop through MySQL results easily?
Accessing Mac applications from Ruby or PHP or Cocoa
How do I run PHP code when a user clicks on a link?

Categories

HOME
amazon-web-services
extjs
powerbi
weblogic12c
schema.org
tcsh
filesize
hdfs
odata
docker-cloud
flurry
haxe
position
firebase-dynamic-links
native-base
polymer-2.x
lc3
fullcalendar-scheduler
complexity-theory
supervisord
pinterest
cocos2d-android
gmp
parceler
ipa
webviewclient
beego
cube
region
jsonresult
argv
kendo-listview
express-session
transform
grails-2.5
orchardcms-1.8
wdk
ftp-server
telephonymanager
newtons-method
oauth2-playground
viewcontroller
pyttsx
vungle-ads
haar-classifier
reverse-dns
section508
spring.net
maven-jaxb2-plugin
popen
dex
swiperefreshlayout
zend-server
business-rules
kendo-treeview
openerp-6
scheduledexecutorservice
okio
msbuild-4.0
node-glob
gwt-syncproxy
psd
helm
ora-04091
calcite
android-studio-import
shoes
fat32
jolie
dos2unix
juttle
lifetime
nesper
libgcc
full-text-indexing
redpitaya
reference-counting
typesafe-stack
meteorite
armv6
cloo
revmob
indesign-server
sunstudio
xs
days
email-verification
voice-recording
msinfo32
uipangesturerecognizer
snackjs
fragment-identifier
aptitude
jdownloader
android-input-method
principles
aspbutton
browser-based
facebook-fbml
rakudo
icon-language
service-factory

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App