One way is that you could loop over all$xml->source
and exit from the loop when the value isimportant
like so using a foreach loop:
<?php
$xml=simplexml_load_file("data.xml") or die("Error: Cannot create object");
foreach($xml->source as $source){
if($source['mount'] == '/stu'){
echo $source->important;
break;
}
}
?>
With XPath, you describe what you want to select. Starting form the top of the document at the "root node" with/
and then "walk" down the levels of the tree with each "step".
If you are looking for all of theimportant
elements that are children ofsource
elements, which are children ofstats
:
/stats/source/important
Or you could look forimportant
elements anywhere, at any level within the document with:
//important
And if you only want theimportant
element that is a child ofsource
with the attributemount="/stu"
:
/stats/source[@mount="/stu"]/important
Applied:
$result = $xml->xpath('/stats/source[@mount="/stu"]/important');
foreach ($result as $important){
echo '/stats/source[@mount="/stu"]/important: ', $important, "\n";
}
Using DOMDocument & DOMXPath make the task of retrieving particular nodes from within a simple XML file like this quite straightforward.
The XML posted contained errors with thedata
tags as mentioned - these were caught bylibxml
as it has it's own error handling routines.
$xml='<stats>
<data1>lorem</data1>
<data2>ipsum</data2>
<source mount="/mno">
<thing>1.0</thing>
<somethingelse>10</somethingelse>
<important>12345</important>
<more>word</more>
</source>
<source mount="/pqr">
<thing>1.0</thing>
<somethingelse>10</somethingelse>
<important>23456</important>
<more>word</more>
</source>
<source mount="/stu">
<thing>1.0</thing>
<somethingelse>10</somethingelse>
<important>34567</important>
<more>word</more>
</source>
<source mount="/vwx">
<thing>1.0</thing>
<somethingelse>10</somethingelse>
<important>45678</important>
<more>word</more>
</source>
</stats>';
libxml_use_internal_errors( true ) ;
$dom=new DOMDocument();
$dom->validateOnParse=false;
$dom->recover=true;
$dom->strictErrorChecking=false;
$dom->loadXML( $xml );
$errors=libxml_get_errors();
$xp=new DOMXPath( $dom );
if( $xp ){
$expr='//source[@mount="/stu"]/important';
$col=$xp->query( $expr );
if( $col && $col->length > 0 )echo $col->item(0)->nodeValue;
}
The XPath expression$expr='//source[@mount="/stu"]/important';
says to find anysource
node that has an attributemount
that exactly equals the desired string/stu
- considerably more complex matching of nodes and attributes can be done than this.
This outputs:
34567
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.