V
V
Vlad_beg2018-04-15 11:48:00
PHP
Vlad_beg, 2018-04-15 11:48:00

How to correctly access a specific element in an xml file?

There is an xml file https://pastebin.com/AYi12DDx
You need to access the value value, which contains information about the database tables

Tablename
PK	uniqueId
FK1	foreignKey
fieldname

I got access to the values, but all the values ​​are displayed as one element of the array, I would like to access each one individually.
$xml = simplexml_load_file('file.xml');

  foreach($xml->root->mxCell as $item) {
    echo "<pre>";
    print_r($item->attributes()->value);
    echo "</pre>";
  }

When trying to access the second element value gives an error
main(): Cannot add element value number 1 when only 0 such elements exist in

Here is what it currently outputs
SimpleXMLElement Object
(
    [0] => 
Tablename
PK	uniqueId
FK1	foreignKey
fieldname

)
SimpleXMLElement Object
(
    [0] => 
Tablename
PK	uniqueId
FK1	foreignKey
fieldname

fieldname
FK1
foreignKey
FK1
foreignKey

)
SimpleXMLElement Object
(
    [0] => 
Tablename
PK	uniqueId
FK1	foreignKey
fieldname

fieldname
)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
svetlov97, 2018-04-15
@svetlov97

It gives you everything correctly, the data is divided, what do you look at? Use explode() inside foreach, or better after it, so that the script runs faster

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question