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

Discussing Html . Code , Software , other

Moderators: macek, egami, gesf

gogs85
New php-forum User
New php-forum User
Posts: 5
Joined: Tue Apr 17, 2012 6:46 am

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

Postby gogs85 » Tue Apr 17, 2012 6:54 am

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;
}

?>

User avatar
minimihi
New php-forum User
New php-forum User
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

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

Postby minimihi » Tue Apr 17, 2012 3:00 pm

You want to put those DIVs or whole HTML to XML?

gogs85
New php-forum User
New php-forum User
Posts: 5
Joined: Tue Apr 17, 2012 6:46 am

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

Postby gogs85 » Wed Apr 18, 2012 7:22 am

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>

User avatar
minimihi
New php-forum User
New php-forum User
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

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

Postby minimihi » Wed Apr 18, 2012 1:27 pm

As a sample

Code: Select all

...
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

gogs85
New php-forum User
New php-forum User
Posts: 5
Joined: Tue Apr 17, 2012 6:46 am

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

Postby gogs85 » Fri Apr 20, 2012 9:25 am

Thanks and how to save this in xml file, be cose this not make xml file?

User avatar
minimihi
New php-forum User
New php-forum User
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

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

Postby minimihi » Fri Apr 20, 2012 2:22 pm

Generate file content and just write a file.

Reference:
File writing
fwrite()


Return to “HTML Basics”

Who is online

Users browsing this forum: No registered users and 1 guest

cron