如何剥离标签中的标签PHP XML MYSQL GCIDE,同时留下文本 [英] How to strip tags within a tag PHP XML MYSQL GCIDE while leaving text

查看:195
本文介绍了如何剥离标签中的标签PHP XML MYSQL GCIDE,同时留下文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图找出一种将gcide xml文件转换为结构化sql转储的方法,虽然我已经取得了一些成功,但似乎获得的结果有限。

Been trying to figure out a way to turn the gcide xml file to a structured sql dump and although i have had a bit of success it seems that the results which gained are limited.

我需要知道一种在xml文件中的另一个标签中标记的方法。

I need to know a way to strip tags within another tag within an xml file.

eg。

< entry>< def> some def< altname> hey< / altname>< / def>< / entry>

删除altname,但保留值可用于def标签。

remove "altname" but keep the value available for "def" tag.

或使用php删除子节点,同时保持作为字符串的父可用值。

Or using php to remove child nodes while keeping the value available for parent as a string.

尝试将父代转换为字符串,但发现子节点已被删除。还使用mysql加载xml文件,但与结构有相同的问题。

Tried converting parent to string but found that the child nodes were removed. Also used mysql load xml file but had the same issues with structure.

正在使用的GCIDE文件

the GCIDE files being used

< a href =http://rali.iro.umontreal.ca/GCIDE/new-entries.zip =nofollow> http://rali.iro.umontreal.ca/GCIDE/new-entries.zip

如果您能够将文件转换为sql转储,那么将非常感激。

if you are able to convert the files to sql dump then it'd be highly appreciated.

推荐答案

此代码段标签在目标标签()中:

This code strip tags in target tag ():



$str = "<entry><def>some def <altname>hey</altname></def></entry>";

$dom = new domDocument();
$dom -> loadXML($str);

// use getElementsByTagName or use DOMXPath($dom) to find your tag which don't contain other tags
$tags = $dom -> getElementsByTagName("def"); 

$contents = "";
for($i = 0; $tags -> length > $i; $i++){
    $contents = $tags -> item($i) -> nodeValue; //content without tags
    $children = $tags -> item($i) -> childNodes;
    remove_children($tags -> item($i)); //recursively remove chiled nodes
    $tags -> item($i) -> appendChild($dom -> createTextNode($contents));
}

//recursively remove chiled nodes
function remove_children(&$node) {
  while ($node->firstChild) {
    while ($node->firstChild->firstChild) {
      remove_children($node->firstChild);
    }
    $node->removeChild($node->firstChild);
  }
}

echo $dom -> saveXML();

这篇关于如何剥离标签中的标签PHP XML MYSQL GCIDE,同时留下文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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