Skip to content Skip to sidebar Skip to footer

Php To Convert Tag To Custom Xml

I'm teaching myself PHP for a small project. I need to convert all of the tags in a source HTML file (there may be many) into custom XML. I've been trying out things with the DOMD

Solution 1:

Use below code to get XML string:

<?php// We use dom document to load it as an php object$document = new DOMDocument();
$document->loadHTML('<img class="alignnone size-large wp-image-23904" src="https://picnic.ly/wp-content/uploads/2017/01/Screen-Shot-2560-01-27-at-2.32.06-PM-1024x572.png" alt="this is a picture" width="1024" height="574" />');
$img = $document->getElementsByTagName("img")->item(0);
// The Wrapper for your xml$xml = "<image>\n";
for ($i = 0; $i < $img->attributes->length; $i++) {
    $attribute = $img->attributes->item($i);
    $name = $attribute->name;
    $value = $attribute->textContent;
    // Indent the element$xml .= "    ";
    // Create the element$xml .= "<" . $name . ">";
    $xml .= $value;
    $xml .= "</" . $name . ">";
    // Break line at end$xml .= "\n";
}
$xml .= "</image>";
echo$xml;

and the result:

<image><class>alignnone size-large wp-image-23904</class><src>https://picnic.ly/wp-content/uploads/2017/01/Screen-Shot-2560-01-27-at-2.32.06-PM-1024x572.png</src><alt>this is a picture</alt><width>1024</width><height>574</height></image>

Tell me if this is not your want solution or has problem.

EDIT: Best Solution is http://syframework.alwaysdata.net/44j i have created.

Solution 2:

<image><src>the url to the image</src><alt>alt_Description_</alt><description>Add_Image_description</description><class>Image_Class_Add_</class><height>Image_height_in_pixels</height><width>Image_width_in_pixels</width><title>Whatever_title_you_want_for_the_image</title></image>

Post a Comment for "Php To Convert Tag To Custom Xml"