1

I am attempting to parse ALL of the elements from an SVG file into a PHP array. Unfortunately, only a few of the elements are being extracted.

I tried converting the SVG into an array using simplexml_load_string, but when I tried to print_r, only a few elements are echoed to screen.

My attempt:

$xmlstring = file_get_contents('hta_svg_element_test.svg');
$arr = simplexml_load_string($xmlstring);
print_r($arr);

SVG code:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg:svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   width="3600"
   height="4800"
   viewBox="0 0 3600 4800"
   version="1.1"
   id="svg8"
   inkscape:version="0.92.3 (2405546, 2018-03-11)"
   sodipodi:docname="hta_svg_element_test.svg">
  <HTA_GLOBALS
     HTA_PATH_QTY="1"
     HTA_TEXT_QTY="5" />
  <svg:defs
     id="defs2" />
  <svg:metadata
     id="metadata5">
    <rdf:RDF>
      <cc:Work
         rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type
           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
        <dc:title />
      </cc:Work>
    </rdf:RDF>
  </svg:metadata>
  <svg:g
     inkscape:label="Layer 1"
     inkscape:groupmode="layer"
     id="layer1"
     transform="translate(0,973.00002)">
    <svg:rect
       style="opacity:1;vector-effect:none;fill:#7822ff;fill-opacity:1;stroke:none;stroke-width:0.99999994;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
       id="HTA_PATH_1"
       width="1186.1003"
       height="1291.1198"
       x="1056.3706"
       y="775.26257"
       HTA_PATH_MAX_WIDTH="2000"
       HTA_PATH_MAX_HEIGHT="500" />
    <svg:text
       xml:space="preserve"
       style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:133.33332825px;line-height:1.25;font-family:'Drone Ranger 02';-inkscape-font-specification:'Drone Ranger 02, Normal';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-feature-settings:normal;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none"
       x="1025.4825"
       y="27.772198"
       id="HTA_TEXT_1"
       HTA_MAX_WIDTH="500"
       HTA_MAX_HEIGHT="100"><svg:tspan
         sodipodi:role="line"
         id="tspan43"
         x="1025.4825"
         y="27.772198">TEST TEXT</svg:tspan></svg:text>
  </svg:g>
</svg:svg>

My results:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [width] => 3600
            [height] => 4800
            [viewBox] => 0 0 3600 4800
            [version] => 1.1
            [id] => svg8
        )

    [HTA_GLOBALS] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [HTA_PATH_QTY] => 1
                    [HTA_TEXT_QTY] => 5
                )

        )

)

I'm expecting all of the SVG elements within the entire file to be put into the array and be easily accessible. Not just the small set that print_r is printing to screen. Any help is much appreciated.

Wyatt Jackson
  • 303
  • 1
  • 2
  • 11
  • Your problem must be related to XML name spaces in some way. All elements coming with a name space identifier (like – user5329483 May 04 '19 at 18:11
  • @user5329483 Thank you for pointing me in the right direction. I now know I must register my namespaces. But do I have to do a separate for each loop for each namespace path? I was hoping to do 1 one loop for all namespaces within my SVG to generate 1 array... https://stackoverflow.com/questions/16226736/loop-through-svg-elements-with-php/16226762#16226762 – Wyatt Jackson May 04 '19 at 19:16
  • I found this guys solution, but it still misses some of the elements... https://outlandish.com/blog/tutorial/xml-to-json/ – Wyatt Jackson May 04 '19 at 20:45
  • Did you find something? Please post it as an own answer as I struggled on a similar issue. – user5329483 May 11 '19 at 13:58

0 Answers0