从php读取XML标签ID [英] read XML tag id from php

查看:67
本文介绍了从php读取XML标签ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下XML文件

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

在这个XML文件中,我需要使用tagid来获取内容.我真正需要的是当我输入"TopMakeHomepage"时,我需要将其输出为"Mache 4W Consulting Webseite zu deiner Starseite!". ... 请帮我找出来.预先感谢

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

推荐答案

使用 DOM扩展应该是这样的:

$dom = new DOMDocument;
$dom->validateOnParse = TRUE;
$dom->loadXML($xmlString); // or use ->load('file.xml')
$node = $dom->getElementById('foo');
echo $node->nodeValue;

请参阅手册

如果它不能与getElementById一起使用(通常仅在DTD不知道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;
}

getElementById不同, XPath查询始终返回 DOMNodeList .如果查询找不到任何节点,它将为空.

Unlike getElementById, an XPath query always returns a DOMNodeList. It will be empty if the query didn't find any nodes.

如果该ID是真实的XML ID,则还可以在XPath中使用id()函数

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

请参见简化PHP DOM XML解析-如何?有关XML ID的更多详细信息.

See Simplify PHP DOM XML parsing - how? for more details on XML IDs.

这篇关于从php读取XML标签ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆