简体   繁体   English

得到之间的文字 <tags> 在PHP中 </tags>

[英]get text between <tags>in php</tags>

Currently i am using and have looked through this code http://us.php.net/manual/en/function.xml-set-element-handler.php#85970 How do i get the text between tags? 目前我正在使用并查看了此代码http://us.php.net/manual/en/function.xml-set-element-handler.php#85970如何获取标签之间的文本?

i am using php5 and XML Parser 我正在使用php5和XML Parser

This should explain it: 这应该解释一下:

 xml_set_character_data_handler ( $parser, 'tagContent' );

 function tagContent ( $parser, $content )
 {
 }

Don't use PHP's XML Parser unless you know what you are doing - it's a SAX parser which requires you to understand the idea of SAX events. 除非你知道自己在做什么,否则不要使用PHP的XML Parser - 它是一个SAX解析器,它要求你理解SAX事件的概念。 You don't need it unless you need to handle very large XML files very quickly. 除非需要非常快速地处理非常大的XML文件,否则不需要它。 For 99% of cases, you don't need to use it. 对于99%的情况,您不需要使用它。 Based on your question, you are simply opening up an XML file and searching for a particular tag and extracting the strings contained in that tag. 根据您的问题,您只需打开XML文件并搜索特定标记并提取该标记中包含的字符串。 For that, you should use the DOM or SimpleXML parsers. 为此,您应该使用DOM或SimpleXML解析器。 SimpleXML lets you use XPath - see this example code which does broadly what you want. SimpleXML允许您使用XPath - 请参阅此示例代码该代码广泛地满足您的需求。 (If you want to use DOM, you'll have to look for it on the PHP site - I can't post hyperlinks as I'm a newbie - just be careful not to confuse it with DOMXML which is the old PHP4 XML parser.) (如果你想使用DOM,你必须在PHP网站上查找它 - 我不能发布超链接,因为我是新手 - 请注意不要将它与DOMXML混淆,后者是旧的PHP4 XML解析器。)

If I've misread this and you do actually want to learn how to use SAX parsing, Google will help - but the basics theory is this: your code will be processing the complete document as a series of events. 如果我误读了这个并且你确实想学习如何使用SAX解析,谷歌会帮助 - 但基本理论是这样的:你的代码将把整个文档作为一系列事件处理。 Each event will represent something in the document - starting with the beginning of the document, then each element being opened and closed, each attribute being parsed, and each string being parsed. 每个事件都将代表文档中的某些内容 - 从文档的开头开始,然后打开和关闭每个元素,解析每个属性,并解析每个字符串。 You need to have code that listens to the events you are interested in - when an element gets opened and closed - and then from that, see if the element that's being opened is the one you want - if it is, you need to then tell the event handler that handles the text that it should now listen for text and store whatever text you are looking for into a variable. 你需要有代码来监听你感兴趣的事件 - 当一个元素被打开和关闭时 - 然后从那里,看看正在打开的元素是你想要的那个 - 如果是,你需要告诉它处理文本的事件处理程序,它现在应该监听文本并将您要查找的任何文本存储到变量中。 When you are done, you can then close the stream off and do whatever you want to do with the text. 完成后,您可以关闭流并对文本执行任何操作。

Again, based on the question, it sounds like you need DOM or SimpleXML not SAX. 同样,基于这个问题,听起来你需要DOM或SimpleXML而不是SAX。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM