Xml data extraction using php

Source: Internet
Author: User
I know that php has many ways to retrieve data from xml through php. the most convenient way is to use regular expressions. but what should I do if I use DOM?
I am not very familiar with Baidu. please explain it to me.
For example

20110524

13.82

13.94

13.79

13.85


20110525

13.82

13.86

13.58

13.60


What should I do if I want to get the corresponding value through the id attribute of this xml segment?
I am the first:
Preg_match_all ("/\ (.*?) \ <\/Value \>/", $ Result, $ array); Operation after obtaining the Value;
Second:
$ Doc = new DOMDocument ();
$ Doc-> load ('sixx. XML'); // read the xml file
$ Results = $ doc-> getElementsByTagName ("Record ");
Foreach ($ Results as $ Result ){
$ Records = $ Result-> getElementsByTagName ("Item ");
$ Times = $ Records-> item (0)-> nodeValue;
}
Locate Item to continue operation
I want to know what to do if I use the XML id to retrieve data?
I tried getElementById () to get it. the system prompts that this method is not available...
Call to undefined method DOMElement: getElementById ().
Thank you for your help.


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"]');print_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). I want to use SimpleXML to process the document, but why cannot I retrieve the data?
The code is as follows:
$ Xml = simplexml_load_file ('6. XML ');
Foreach ($ xml-> Item as $ ){
Echo $ a-> Value;
}
?>
The running result is empty .. I think the code is okay ..

No problem

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

Use simplexml_load_file or simplexml_load_string to convert xml into an array.
Reference: http://php.net/manual/en/function.simplexml-load-file.php

I read the information on the official website, and then read the subsequent code. I found that the data can be retrieved. However, there is still a problem, that is, the value of the obtained array fails to be obtained by using the id that was first mentioned.

Result (printr_r ($ t )):
Array
(
[0] => SimpleXMLElement Object
(
[@ Attributes] => Array
(
[Id] => 8
)

[Value] = & gt; 13.94
)

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

[Value] = & gt; 13.86
)

)
To obtain specific data,
I used echo $ t-> Value;
And echo $ t-> Id;
Data cannot be obtained.
I am grateful for not understanding XML arrays.

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.