PHP code example of ottosmops / xmltoolkit

1. Go to this page and download the library: Download ottosmops/xmltoolkit library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ottosmops / xmltoolkit example snippets



use Ottosmops\Xmltoolkit;

$xml = new Xmltoolkit();
$xml->loadFromFile('example.xml');
$xml->renameTagByXPath('//a', 'ref');
$xml->saveToFile('example.xml', true); // pretty print

// Find elements by attribute value
$elements = $xml->findElementsByAttributeValue('type', 'special');

// Replace attribute value
$xml->replaceAttributeValue('type', 'special', 'normal');

// Find elements by text content
$elements = $xml->findElementsByTextContent('oldtext');

// Replace text content
$xml->replaceElementTextContent('oldtext', 'newtext');

// Save as pretty-printed string
$prettyXml = $xml->saveToString(true);

$xml = new Ottosmops\Xmltoolkit();
$xml->registerNamespaces(['ns' => 'http://example.com/ns']);
$xml->loadFromFile('example.xml');
$nodes = $xml->queryXPath('//ns:tag');

public function loadFromFile(string $filePath): bool

public function loadFromString(string $xmlString): bool

public function loadFromFragment(string $xmlFragment): bool

public function saveToFile(string $filePath, bool $prettyPrint = false): bool

public function saveToString(bool $prettyPrint = false): string

public function findElementsByAttributeValue(string $attributeName, string $attributeValue): array

public function replaceAttributeValue(string $attributeName, string $oldValue, string $newValue): void

public function findElementsByTextContent(string $textContent): array

public function replaceElementTextContent(string $oldText, string $newText): void

public function renameTagByXPath(string $xpathExpression, string $newTagName): 
void

public function renameAttributeByXPath(string $xpathExpression, string $oldAttributeName, string $newAttributeName): void

public function addAttributeByXPath(string $xpathExpression, string $attributeName, string $attributeValue): void

public function removeAttributeByXPath(string $xpathExpression, string $attributeName): void

public function queryXPath(string $xpathExpression): array

public function appendHtmlToXPath(string $xpathExpression, string $htmlString): void


    public function removeElementByXPath(string $xpathExpression): void

public function wrapElementByXPath(string $xpathExpression, string $wrapperTagName): void

public function unwrapElementByXPath(string $xpathExpression): void

public function findElementsByAttributeRegex(string $attributeName, string $pattern): array

public function replaceAttributeValueRegex(string $attributeName, string $pattern, string $replacement): void

public function findElementsByTextRegex(string $pattern): array

public function replaceElementTextRegex(string $pattern, string $replacement): void

$elements = $xml->findElementsByAttributeRegex('type', '/^sp.*/');
$xml->replaceAttributeValueRegex('type', '/^sp.*/', 'normal');
$elements = $xml->findElementsByTextRegex('/old.*/');
$xml->replaceElementTextRegex('/old.*/', 'newtext');