Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read XML tag id from php

Tags:

php

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 &amp; Umwandlung|Altsystem Dokumentation|Daten Umwandlung &amp; Migration|Erstellen von Datenbeschreibungsverzeichnis|System- &amp; Anwendungs Support|Projekt Management &amp; 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

like image 663
Sakthivel Avatar asked Jun 14 '10 06:06

Sakthivel


3 Answers

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');
}
like image 155
Radek Simko Avatar answered Oct 22 '22 04:10

Radek Simko


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 id

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

like image 26
Gordon Avatar answered Oct 22 '22 03:10

Gordon


For SimpleXML this should do the trick:

$xml = simplexml_load_file($xmlFileLoc);
foreach ($xml->Message as $msg)
{
    echo $msg['Id'];
}
like image 40
Rene Pot Avatar answered Oct 22 '22 03:10

Rene Pot