i am have the following XML file
<?xml version="1.0" encoding="iso-8859-1"?>
<Message Id="Language">German</Message>
<Message Id="LangEnglish">German</Message>
<Message Id="TopMakeHomepage">
Mache 4W Consulting Webseite zu deiner Starseite!
</Message>
<Message Id="TopLinkEmpSec">
4W Mitarbeiter
</Message>
<Message Id="TopLinkFeedback">
Feedback
</Message>
<Message Id="TopLinkSiteMap">
Site Map
</Message>
<Message Id="TopLinkContactUs">
Kontakt
</Message>
<Message Id="TopSetLangEn">
ins Englische
</Message>
<Message Id="TopSetLangDe">
ins Deutsche
</Message>
<Message Id="TopSetLangEs">
ins Spanische
</Message>
<Message Id="MenuLinks">
!~|4W Starseite|Company|Über uns|Kontakt|4W anschließen|Services|Kunden Software Entwicklung|Altsystem Neugestalltung & Umwandlung|Altsystem Dokumentation|Daten Umwandlung & Migration|Erstellen von Datenbeschreibungsverzeichnis|System- & Anwendungs Support|Projekt Management & Planunng|Personal Erweiterung|Projekt Ausgliederung|Mitarbeiter Ausbildung|Technologie|Intersystems Caché|M / MUMPS|Zusätzliche Technologien|Methodologie|Feedback|~!
</Message>
</MsgFile>
in this XML file i need to fetch the contents using the tagid . what exactly i need is when i input the 'TopMakeHomepage' i need output as 'Mache 4W Consulting Webseite zu deiner Starseite!' ... Please help me to find out this . Thanks in advance
Use SimpleXML:
$xml = simplexml_load_file($grabUrl);
foreach ($xml->Message as $message) {
echo $message->attributes()->Id.'<br />';
}
Or use XMLReader, with which you can miss memory leaks when processing large XMLs.
$xml = new XMLReader;
$xml->open($grabUrl);
while ($xml->read()) {
if ($xml->nodeType === XMLReader::ELEMENT && $xml->name == 'Message')
echo $xml->getAttribute('Id');
}
With the DOM extension it should be something like this:
$dom = new DOMDocument;
$dom->validateOnParse = TRUE;
$dom->loadXML($xmlString); // or use ->load('file.xml')
$node = $dom->getElementById('foo');
echo $node->nodeValue;
See the manual on
DOMDocument::getElementById
— Searches for an element with a certain idIf it doesn't work with getElementById
(which usually only happens if the DTD doesn't know the id attribute), you can still use XPath to do the query:
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//Message[@id = "foo"]');
foreach($nodes as $node) {
echo $node->nodeValue;
}
Unlike getElementById
, an XPath query always returns a DOMNodeList
. It will be empty if the query didn't find any nodes.
If the ID is a real XML ID, you can also use the id() function in XPath
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('id("foo")');
foreach($nodes as $node) {
echo $node->nodeValue;
}
See Simplify PHP DOM XML parsing - how? for more details on XML IDs.
For SimpleXML this should do the trick:
$xml = simplexml_load_file($xmlFileLoc);
foreach ($xml->Message as $msg)
{
echo $msg['Id'];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With