PHP & XML: SimpleXML, DOMDocument

738 views
651 views

Published on

Doua tehnologii simple pentru lucrul cu XML in PHP: SimpleXML si DOMDocument (care poate parsa inclusiv fisiere HTML cu XML invalid).

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
738
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • PHP & XML: SimpleXML, DOMDocument

    1. 1. DevChallenge 000000012010-12-18T10:15:00+02:00
    2. 2. DevChallenge 000000012010-12-18T10:15:00+02:00presentation byValentin Bora
    3. 3. <XML />
    4. 4. SimpleXML
    5. 5. <myxml> <masini> <masina> <caroserie> <material>Plastic</material> <textura></textura> </caroserie> </masina> <masina> <caroserie> <material>Tabla</material> <textura></textura> </caroserie> </masina> </masini></myxml>$xml = new SimpleXMLElement($xml_string);foreach ($xml->masini->masina as $masina) { $masina->addAttribute(‘culoare’, “negru”); $masina->caroserie->material = “Plastic”;}
    6. 6. SimpleXML <myxml> <masini> <masina> <caroserie> <material>Plastic</material> <textura></textura> </caroserie> </masina> <masina> <caroserie> <material>Tabla</material> <textura></textura> </caroserie> </masina> </masini> </myxml> $xml = new SimpleXMLElement($xml_string); foreach ($xml->masini->masina as $masina) { $masina->addAttribute(‘culoare’, “negru”); $masina->caroserie->material = “Plastic”; }
    7. 7. <myxml> <masini> <masina> <caroserie> <material>Plastic</material> <textura></textura> </caroserie> </masina> <masina> <caroserie> <material>Tabla</material> <textura></textura> </caroserie> </masina> </masini></myxml>$xml = new SimpleXMLElement($xml_string);foreach($xml->xpath(’//caroserie’) as $caroserie) { // Do stuff }
    8. 8. SimpleXML <myxml> <masini> <masina> <caroserie> <material>Plastic</material> <textura></textura> </caroserie> </masina> <masina> <caroserie> <material>Tabla</material> <textura></textura> </caroserie> </masina> </masini> </myxml> $xml = new SimpleXMLElement($xml_string); foreach($xml->xpath(’//caroserie’) as $caroserie) { // Do stuff }
    9. 9. $dom = new DOMDocument();$dom->loadHTMLFile(‘http://www.example.com’);$simple = simplexml_import_dom($dom);foreach($simple->xpath(’//div[@id=”something”]’) as $div) { // Do stuff }
    10. 10. DOMDocument $dom = new DOMDocument(); $dom->loadHTMLFile(‘http://www.example.com’); $simple = simplexml_import_dom($dom); foreach($simple->xpath(’//div[@id=”something”]’) as $div) { // Do stuff }

    ×