一直想找到一种方法将gcide xml文件转换为结构化的sql转储,尽管我已经取得了一些成功,但似乎获得的结果有限。
我需要知道一种在xml文件中剥离标签的方法。
例如
<entry><def>some def <altname>hey</altname></def></entry>
删除“altname”但保留“def”标记的值。
或者使用php删除子节点,同时保持父值可用作字符串。
尝试将父级转换为字符串,但发现子节点已被删除。还使用了mysql load xml文件,但结构也有同样的问题。
正在使用的GCIDE文件
http://rali.iro.umontreal.ca/GCIDE/new-entries.zip
如果你能够将文件转换为sql转储,那么我们将非常感激。
答案 0 :(得分:0)
此代码条标记在目标标记()中:
$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();