Reputation: 397
Here is Sample XML file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Catalog SYSTEM "http://store.yahoo.com/doc/dtd/Catalog.dtd">
<Catalog Store`enter code here`ID="yhst-69328165909994" StoreName="Inmod Modern Furniture" PublishTimestamp="1517733517">
<Table ID="main-table-for-download">
<TableField ID="name" Type="text"/>
<TableField ID="sale-price" Type="numbers"/>
<TableField ID="path" Type="text"/>
<TableField ID="taxable" Type="yes-no"/>
<TableField ID="ship-weight" Type="numbers"/>
<TableField ID="price" Type="numbers"/>
<TableField ID="orderable" Type="orderable"/>
<TableField ID="code" Type="text"/>
</Table>
I just want to get this element after reading that XML file.I need to check if that XML file contains yahoo feed or not?
you can suggest any better option for that too.
My Attempt :
$reader = new XMLReader();
$reader->xml($myxmlfilecontent);
while ($reader->read()) {
if ($reader->nodeType == XMLReader::ELEMENT) {
while ($reader->read()) {
if ($reader->nodeType == XMLReader::ELEMENT) {
//array_push($nodeList, $reader->localName);
if (stripos($xml, 'yahoo') !== false) {
echo 'yahoo read';die
}else{
echo 'not yahoo read';die
}
}
}
}
}
Basically in this example i am searching yahoo in whole file, i don't want to search in whole file i just want to get doc type element and search on that string.
Upvotes: 0
Views: 744
Reputation: 57121
You can check the node type as being a DOC_TYPE
node and then fetch the xml for that node using readOuterXml()
...
$reader = new XMLReader();
$reader->xml($myxmlfilecontent);
while ($reader->read()) {
if ($reader->nodeType == XMLReader::DOC_TYPE) {
echo $reader->readOuterXml();
}
}
This outputs -
<!DOCTYPE Catalog SYSTEM "http://store.yahoo.com/doc/dtd/Catalog.dtd">
Upvotes: 1
Reputation: 163352
Instead of XMLReader, you could use DOMDocument and get the systemId:
$doc = new DOMDocument();
$doc->load($myxmlfilecontent);
$systemId = $doc->doctype->systemId;
if (stripos($systemId, 'yahoo') !== false) {
echo "yahoo read";
} else {
echo "not yahoo read";
}
Upvotes: 1