Vinod Sebastian – B.Tech, M.Com, PGCBM, PGCPM, PGDBIO

Hi I'm a Web Architect by Profession and an Artist by nature. I love empowering People, aligning to Processes and delivering Projects.

Advertisements

XML

XML stands for Extended Markup Language. It is a markup language that defines rules for encoding documents in a format that is both human-readable and machine-readable.

  • DTD or Document Type Definition provides information about the structure and content of a particular XML document.
  • An entity in XML is a named storage unit, similar to a tag, used to define reusable content.
  • An element in XML typically consists of attributes and text content.
  • The content or value of an entity in XML refers to the actual data stored within the entity.

SimpleXML

SimpleXML is a PHP 5 library specifically designed for working with XML data. It simplifies the process of parsing and manipulating XML documents.


$library = new SimpleXMLElement($xmlstr); // Load XML from a string.
$library = new SimpleXMLElement('library.xml', null, true); // Load XML from a file.

foreach ($library->book as $book) {
    echo $book['isbn'] . "n"; // Accessing an attribute.
    echo $book->title . "n"; // Accessing a child element.
}

foreach ($library->children() as $child => $valuechild) {
    echo $child->getName() . "=" . $child;
    echo $child . "=" . $valuechild;
    foreach ($child->attributes() as $attr => $valueattr) {
        echo $attr->getName() . "=" . $attr;
        echo $attr . "=" . $valueattr;
    }
}
    

XPATH

XPath (XML Path Language) is a query language used for navigating through and selecting nodes in an XML document.


$results = $library->xpath('/library/book/title'); // Returns the XML object defined by the XPath expression.

$newbook = $book->addChild('title', 'Enders Game'); // Adding a child element with a specified value.
$book->addAttribute('isbn', 0812550706); // Adding an attribute to an element.

header('Content-type: text/xml'); // Setting the header for XML content.
echo $library->asXML(); // Output the XML content.

// Additional operations such as removing elements and working with namespaces can also be performed using XPath.
    

DOM

DOM (Document Object Model) in PHP provides a way to represent and interact with structured documents like XML and HTML.


$dom = new DomDocument();

$dom->load('library.xml'); // Load XML content from a file.
$dom->loadXML($xml); // Load XML content from a string.

$dom->loadHTMLFile('library.html'); // Load HTML content from a file.
$dom->loadHTML($html); // Load HTML content from a string.

$dom->saveXML(); // Save the document as XML.
$dom->saveHTML(); // Save the document as HTML.

$element = $dom->createElement('a', 'This is a link!'); // Create a new element.
$element = $dom->getAttribute('target'); // Get the value of an attribute.
$element = $dom->hasAttribute('target'); // Check if an attribute exists.
$element = $dom->setAttribute('target', '_blank'); // Set a new attribute.
$element = $dom->removeAttribute('target'); // Remove an attribute.
    
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x