Sakthivel
Sakthivel

Reputation: 1941

read XML tag id from 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

Upvotes: 5

Views: 11388

Answers (3)

Gordon
Gordon

Reputation: 317217

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


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.

Upvotes: 4

Rene Pot
Rene Pot

Reputation: 24815

For SimpleXML this should do the trick:

$xml = simplexml_load_file($xmlFileLoc);
foreach ($xml->Message as $msg)
{
    echo $msg['Id'];
}

Upvotes: 2

Radek Simko
Radek Simko

Reputation: 16146

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

Upvotes: 5

Related Questions