how to html value export in xml file vith tag in php

A

Anonymous

Guest
Dears,
how to html value export in xml file with tag in php. This code read all div value from site:

<?php
$some_link = 'http://www.popusti.rs/';
$tagName = 'div';
$attrName = 'class';
$attrValue = 'offer-list-item';

$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
@$dom->loadHTMLFile($some_link);

$html = getTags( $dom, $tagName, $attrName, $attrValue );
echo $html;

function getTags( $dom, $tagName, $attrName, $attrValue ){
$html = '';
$domxpath = new DOMXPath($dom);
$newDom = new DOMDocument;
$newDom->formatOutput = true;

$filtered = $domxpath->query("//$tagName" . '[@' . $attrName . "='$attrValue']");
$filtered = $domxpath->query('//div[@class="offer-list-item"]');
// '//' when you don't know 'absolute' path

// since above returns DomNodeList Object
// I use following routine to convert it to string(html); copied it from someone's post in this site. Thank you.
$i = 0;
while( $myItem = $filtered->item($i++) ){
$node = $newDom->importNode( $myItem, true ); // import node
$newDom->appendChild($node); // append node
}
$html = $newDom->savehtml();
return $html;
}

?>
 
example:
<div class ="offers"> some text</div>
<div class = "pictures"> some link</div>
<div class = "price"> 200$</div>

i want to make xml file like this example:
<my offers>
<offers>some text</offers>
<pictures>some link</pictures>
<price> 200$</price>
</my offers>
 
As a sample
PHP:
...
while ($myItem = $filtered->item($i++)) {
    $node = $newDom->importNode($myItem, true); // import node
echo "Attribute name value: ". $node->getAttribute('name') ."<br/>";
echo "Element value: ". $node->nodeValue ."<br/><br/>";
    $newDom->appendChild($node); // append node
}
... 

See what I use at echo statements.
With this it should be easy to format XML string.

If you want to get value for complete element (eg. input), you need to use $node ->getAttribute('value') instead of $node->nodeValue

Complete element would look like that: <input ... /> (closes at same tag)
Opened element with closing looks like that: <div ...> ... </div> (have opening and closing tags)

Reference:
DOMNode class:
    $nodeValue
    DOMElement::getAttribute
 
Thanks and how to save this in xml file, be cose this not make xml file?
 
Back
Top