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

PHP Regular Expression, Replace html <i> tag to <img> [closed]
Symfony Doctrine query builder LIKE not working in PostgreSQL
Wordpress. Making article items bigger
How to make a dynamic css with php by using a while?
How to stop PHP script and ask user yes or not
How to encrypt laravel 5.2 URL or Routes?
How does stripe connect auth and capture works with PHP?
The specified DSN contains an architecture mismatch between the Driver and Application,
How to use the result of one SQL query in another query (PHP)
WordPress: How to get new post tags
Random nulls after json_encode
Prestashop migration, index.php doesn't get executed
Windows Xampp fatal error when running aws sdk
Read realtime whole output in stream
How can use vue-admin framework with laravel 5.3?
How to convert year format YY into YYYY in PHP [duplicate]

Categories

HOME
amazon-web-services
flask
shinyapps
xcode8.1
applescript
enums
swi-prolog
hdfs
initialization
informatica
capistrano
scheduler
kurento
siesta
getopenfilename
circleci
jquery-file-upload
squirrel-sql
sql-injection
complexity-theory
temperature
google-tasks-api
closures
word2vec
significance
tableview
sharedpreferences
candlestick-chart
cell
web-push
sparkle
audit
progid
steam-web-api
delayed-job
knights-tour
gsp
nest
installanywhere
strapi
django-smart-selects
right-click
game-center
google-prediction
latexml
transparency
sonicmq
gnome-terminal
stateless-session-bean
dblink
kendo-editor
http4s
scheduledexecutorservice
libharu
always-on-top
carmen
msbuild-4.0
node.js-connect
swiffy
verisign
gdl
bayesglm
juniper-network-connect
self
ruby-on-rails-4.1
openshift-cartridge
http.client
dsa
alwayson
kefir.js
erlog
azure-xplat-cli
text-align
python-c-extension
uno
iodocs
utf8-decode
relationships
webmethod
coding-efficiency
thruway
air-native-extension
preorder
pre
map
spring-social-linkedin
iosched
armv6
eclim
django-facebook
ax
dynamic-rdlc-generation
web-frameworks
android-authenticator
examine
jsplitpane
simplecov
ip-geolocation
yuidoc
vows
office-automation
database-deadlocks
kohana-auth
photo-management
3des
formal-semantics
tabbarcontroller
dm
.net-client-profile
main-method
motif
regioninfo

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App