Profile Picture
IT Made Easy
The site provides some rough IT Notes that make your life easier. The site currently has notes on HTML, CSS, Regex, OOPS, JS, JQuery, PHP, Java, ASP.NET, C#, Database, Linux, URL Rewrite, SEO, Project Management and Hosting. Please do share this site if you like.

XML

Next
Next

XML

XML is Extended Markup Language

  • DTD or Document Type Definition gives information about structure and content of the particular XML
  • An entity is a named storage using like say head tag
  • An element usually has attributes amd other texts
  • Finally there is the content or value of the entity


SimpleXML

SimpleXML is a PHP 5 library that takes care of working with XML. Some methods after 5.1.3


$library = new SimpleXMLElement($xmlstr);            //Load XML from string

$library = new SimpleXMLElement(library.xml, NULL, true);   //Load XML from file. True tells first parameter is a path to the file

foreach ($library->book as $book)            //Looping through many book is library. Children

{

echo $book[isbn] . "\n";                  //Accessing an attribute

echo $book->title . "\n";                  //Accessing a child

}

foreach ($library->children() as $child => $valuechild)      //Get children of this element

{

echo $child->getName()."=".$child;            //Example a = this is a link

echo   $child."=". $valuechild;               //Example a = this is a link            

foreach ($child->attributes() as $attr => $valueattr)      //Get attributes of this element

{

echo $attr->getName()."=".$attr;               //Example target = _blank

echo   $attr."=". $valueattr;               //Example target = _blank

}

}


XPATH

XPath or XML Path Language is a way to access and search XML Documents

$results = $library->xpath(/library/book/title);         //Returns the XML Object under the XPath


$newbook = $book->addChild(title, "Enders Game");      ///Adding a child. Third optional parameter is namespace

$book->addAttribute(isbn, 0812550706);         //Adding an attribute. Third optional parameter is namespace

header(Content-type: text/xml);               //Header for XML

echo $library->asXML();                  //Print XML


$library->book[0] = NULL;               //Remove the children of book[0].   You cannot remove an attribute of book. You can only set them to empty. For that                        to happen we will ahve to export to DOM and use a powerfl fnctionality there


$namespaces = $library->getDocNamespaces();         //Gets namespaces declared in the root element referenced. By passing true all namespaces declared recursively in                        children will be returned

foreach ($namespaces as $key => $value)

{

echo "{$key} => {$value}";

}

$namespaces = $library->getNamespaces(true);         //Gets namespaces used (not declared) in the root element referenced. Passing True will return all namespaces used                        recursively in children will be returned


DOM

DOM is Document Object Model


$dom = new DomDocument();

$dom->load(library.xml);               //Load from file

$dom->loadXML($xml);                  //Load from XMl string

$dom->loadHTMLFile("librabry.html");            //Load from HTML file

$dom->loadHTML($html);               //Load from HTML string


$dom->saveXML();                  //Save as XMl

$dom->saveHTML();                  //Save as HTML



$element = $dom->createElement("a", 'This is a link!');      //Creates an element

$element = $dom->getAttribute("target");            //Returns value of attribute

$element = $dom->hasAttribute("target");            //Checks to see if attribute exists. Returns TRUE or FALSE

$element = $dom->setAttribute("target","_blank");      //Adds new attribute. Returns sub DOM object

$element = $dom->:removeAttribute("target");         //Removes attribute. Returns TRUE or FALSE

IT Notes

by VinodSebastian



IT Downloads


Catalogue