dongyu1918
dongyu1918
2012-05-07 21:28
浏览 35

SVG将组移至其他组

Want to move a group into another group, but on some way it ends up in a huge execution time, and the script is never ending. the problem is in the foreach loop rest function ok. I don't see any mistake

<?php
// fouten laten zien
error_reporting(~0); 
ini_set('display_errors', 1); 

// test bestand
$bestand = "00000000346.svg";

// maak nieuwe dom
$dom = new DOMDocument; 

// laat svg in dom
$dom->load($bestand);

// krijg svg element
$svg = $dom->getElementsByTagName("svg")->item(0);

// toevoegen link attribute
$svg->setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");

// maak script tag
$script = $dom->createElement("script");

// zet script link
$script->setAttribute("xlink:href", "svg_zoom_pan.js");

// toevoegen aan svg
$dom->documentElement->appendChild($script);

// maak groep tag
$svgroot = $dom->createElement("g");

// zet een id
$svgroot->setAttribute("id", "svgroot");

// toevoegen aan svg
$dom->documentElement->appendChild($svgroot);

// krijg alle groepen
$groups = $dom->getElementsByTagName("g");

// doorloop groepen
foreach($groups as $group) {

    if($group->getAttribute("id") != "svgroot") {

        // clone groep
        $cloned = $group->cloneNode(true);

        // toevoegen aan svgroot groep  
        $dom->getElementById("svgroot")->appendChild($cloned);

    }
}

// opslaan bestand
$dom->save("svg_" . $bestand);

?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dsrnwngq411594
    dsrnwngq411594 2012-05-13 06:51
    已采纳

    If have find the solution to move:

    // doorloop groepen
    foreach($groups as $group) {
    
        // verplaats alleen de andere groepen maar niet de root 
        if($group->getAttribute("id") != "svgroot") {
    
            // verplaats groep in svgroot       
            $moved = $group->parentNode->removeChild($group);
    
            // toevoegen aan svgroot groep  
            $firstgroup->appendChild($moved);
    
        }
    }
    

    But it mesh up the svg code?

    part of original svg:

    <?xml version="1.0" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" width="595.000" height="841.000" viewBox="0.0 0.0 595.000 841.000">
      <title>Produce by DWG to SVG Converter 2012 MX</title>
      <desc>Produce by DWG to SVG Converter 2012 MX</desc>
    <g id="000-Onderlegger" display="visible">
    <g><path d="M382.47 595.5L382.47 404.1"
    fill="none" stroke="RGB(0,124,165)" stroke-width="0.709"/>
    </g>
    <g><path d="M382.47 404.1L384.45 404.1"
    fill="none" stroke="RGB(0,124,165)" stroke-width="0.709"/>
    </g>
    

    part of the parsed code:

    <?xml version="1.0" standalone="no"?>
    <?xml-stylesheet href="svg.css" type="text/css"?>
    <svg xmlns="http://www.w3.org/2000/svg" width="595.000" height="841.000" viewBox="0.0 0.0 595.000 841.000" onload="init(evt)" xmlns:xlink="http://www.w3.org/1999/xlink">
      <script xlink:href="svg-zoom-pan.js"/>
      <g id="svgroot">
        <g id="000-Onderlegger" display="none">
          <g>
            <path d="M382.47 404.1L384.45 404.1" fill="none" stroke="RGB(0,124,165)" stroke-width="0.709"/>
          </g>
          <g>
            <path d="M384.45 396.46L382.47 396.46" fill="none" stroke="RGB(0,124,165)" stroke-width="0.709"/>
          </g>
    

    here the difference:

    <g>
    <path d="M382.47 595.5L382.47 404.1" fill="none" stroke="RGB(0,124,165)" stroke-width="0.709"/>
    </g>
    
    <g>
    <path d="M382.47 404.1L384.45 404.1" fill="none" stroke="RGB(0,124,165)" stroke-width="0.709"/>
    </g>
    

    Have someone any idea why this happend?

    点赞 评论
  • duankeng1911
    duankeng1911 2012-05-08 11:47

    The getElementsByTagName returns a live list of nodes. That means that when you add more groups to your document the variable $groups will be updated to contain those new groups.

    Since you are looping over the live list of elements and adding one more element in each iteration, the live list be longer and the loop will never end.

    点赞 评论

相关推荐