Extracting XML data problems from PHP

Source: Internet
Author: User
I know that PHP has many ways to get data from XML, most conveniently through regular expressions, but what if I use DOM methods?
I Baidu a bit, not very understand, ask the great God to explain.
Say

20110524

13.82

13.94

13.79

13.85


20110525

13.82

13.86

13.58

13.60


This paragraph of XML, I want to get the corresponding value by the id attribute what should I do?
I am now the first kind:
Preg_match_all ("/\ (.*?) \<\/value\>/", $Result, $array); Operation after getting the value;
The second type:
$doc = new DOMDocument ();
$doc->load (' sixx.xml '); Reading an XML file
$Results = $doc->getelementsbytagname ("Record");
foreach ($Results as $Result) {
$Records = $Result->getelementsbytagname ("Item");
$times = $Records->item (0)->nodevalue;
}
Navigate to item to continue operation
is not using the ID, I would like to know if the use of XML ID data change what to do?
I have tried to get it with getElementById (), prompting without this method ...
Call to undefined method Domelement::getelementbyid ().
Ask the great God to help answer, thank you.


Reply to discussion (solution)

 $s =<<< XML 
    
     
      
      
        20110524  
      
      
      
      
        13.82 
       
      
      
      
        13.94
     
       
      
      
      
        13.79 
       
      
      
      
       
   13.85 
       
      
     
     
      
      
        20110525 
       
      
      
      
        13.82 
       
      
      
      
        13.86 
        
       
       
         13.58 
        
       
       
       
         13.60 
         
       
     
     
    XML; $xml = simplexml_load_string ($s), $t = $xml->xpath ('//*[@Id = "8"] ');p rint_r ($t); 
Array (    [0] = simplexmlelement Object        (            [@attributes] = = Array                (                    [Id] = 8                )            [ Value] = 13.94        )    [1] = = SimpleXMLElement Object        (            [@attributes]                = = Array (                    [Id] = > 8                )            [Value] = 13.86        ))



This is my XML file (the file name is 6.xml) and I want to use SimpleXML to process the document, but why not get the data?
The code is as follows:
$xml = simplexml_load_file (' 6.xml ');
foreach ($xml->item as $a) {
Echo $a->value;
}
?>
The result of the run is empty. I think the code is fine.

No problem

 
   Item as $a) {     echo $a->value. '
';}
2011052413.8213.9413.7913.852011052513.8213.8613.5813.60

You can convert XML to an array using Simplexml_load_file or simplexml_load_string.
Reference: http://php.net/manual/en/function.simplexml-load-file.php

I looked at the information of the Xia Guan Net, then also looked at the next code, found that can take out the data. However, there is still a problem, that is, the first mention of the use of ID to take out the value, the resulting array after the failure.

Based on the results between (Printr_r ($t)):
Array
(
[0] = = simplexmlelement Object
(
[@attributes] = = Array
(
[Id] = 8
)

[Value] = 13.94
)

[1] = = SimpleXMLElement Object
(
[@attributes] = = Array
(
[Id] = 8
)

[Value] = 13.86
)

)
To get specific data,
I am using echo $t-Value;
and Echo $t-Id;
Data is not available.
XML array Some do not understand, but also look more advice, grateful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.