How to strip tags within a tag PHP XML MYSQL GCIDE

2020-08-01 07:07发布

问题:

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.

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>

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

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

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.

the GCIDE files being used

http://rali.iro.umontreal.ca/GCIDE/new-entries.zip

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

回答1:

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();