【问题标题】:Load a svg file(xml) and extract specific information using javascript加载 svg 文件(xml)并使用 javascript 提取特定信息
【发布时间】:2013-04-01 07:40:57
【问题描述】:

我正在尝试使用 jquery/javascript 获取 svg 文件 svg 示例:

<svg width="111" height="123" xmlns="http://www.w3.org/2000/svg">
 <g>
  <title>Layer 1</title>
  <rect fill="#ffffff" stroke="#000000" stroke-width="3" x="1.5" y="1.5"width="108.00001" height="119.99999" id="svg_1" rescale="none" move="Static"/>
  <text text_pos="midcenter" xml:space="preserve" text-anchor="middle" font-family="Fantasy" font-size="14" id="Actor Role name" y="68" x="55" stroke-width="0" stroke="#000000" fill="#000000" rescale="none" move="Static">Actor Role</text>
</g>
</svg>      

并使用类似的方法从文件中提取数据

     $.get(file_url, function(data) {
              var teste=data;
           },'xml')// or use text instead of xml

然后获取所有元素,如 rect 或 text 并说得到类似的东西(排除内部''只是为了知道值来自哪里):

'元素'矩形,'重新缩放'无,'移动'静态

对于文本(不包括里面的''): 'Element' rect, 'rescale' none, 'move' static, 'text_pos' midcenter ,'id' Actor Role name, 'node value' Actor Role

部分解决

    $.get(file_url, function(data) {
       var teste=data; //all data
       rect1=$('<g>').append($(teste).find("text").attr("id")).html();
       rect2=rect1+"-"+$('<g>').append($(teste).find("text").attr("text_pos")).html();
       alert(rect2);
    });
    alert(rect2);

问题发现它没有通过 $.get 之外的变量数据

首先alert(rect2); 给出正确的数据

第二个alert(rect2); 给我未定义

任何人都知道它为什么不提供全局变量:X 已经尝试在外面制作变量但也不起作用

很抱歉忘记更改评论:f 现在它是正确的

【问题讨论】:

  • 你的代码中没有alert(rect1),只有2个alert(rect2)。 2nd 未定义是完全正常的,因为:1- 它在上述函数中定义 2- 它在 ajax 回调中定义(即...异步)
  • 我更改了代码区域但忘记更改其余部分,现在它是正确的

标签: javascript jquery xml svg


【解决方案1】:

这就是我加载 SVG 文件的方式:

$( document ).ready( function() {
    $.get("path-to-svg.svg", null, function(data){
        var svgNode = $("svg", data);
        var docNode = document.adoptNode(svgNode[0]);
        var pageNode = $("#element-to-hold-svg");
        pageNode.html(docNode);
    }, 'xml');
});

【讨论】:

    【解决方案2】:

    我将使用此处找到的代码:Ajax or JavaScript: Changing Style According to Server Response

        var XMLrequest = newXMLHttpRequest(); // new XML request
        XMLrequest.open("GET", myURL, false); // URL of the SVG file on server
        XMLrequest.send(null); // get the SVG file
    
        var mySVG = XMLrequest.responseXML.getElementsByTagName("svg")[0];
    

    所以您现在可以使用jQuery(mySVG) 或纯javascript 来提取属性。

    【讨论】:

      【解决方案3】:

      来自文档:http://api.jquery.com/jquery.parsexml/

      var xml = "<rss version='2.0'><channel><title>RSS Title</title></channel></rss>",
      xmlDoc = $.parseXML( xml ),
      $xml = $( xmlDoc ),
      $title = $xml.find( "title" );
      

      在我的例子中,svg 数据包括

      <?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
      

      所以对我来说,获取 SVG 是这样完成的:

      var xml = "<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="2" height="2"><path fill="none" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" d="M 1 1 l 1 1"/></svg>",
      xmlDoc = $.parseXML( xml ),
      $xml = $( xmlDoc ),
      $svg = $xml.find( "svg" );
      

      虽然有人问过'g'元素。进行了快速测试,但无法使用您提供的数据获得结果,尽管以下示例代码应该有所帮助:

      var xml = '<?xml version="1.0" standalone="no"?><\!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="5cm" height="5cm">  <desc>Two groups, each of two rectangles</desc>  <g id="group1" fill="red">    <rect x="1cm" y="1cm" width="1cm" height="1cm"/>    <rect x="3cm" y="1cm" width="1cm" height="1cm"/>  </g>  <g id="group2" fill="blue">    <rect x="1cm" y="3cm" width="1cm" height="1cm"/>    <rect x="3cm" y="3cm" width="1cm" height="1cm"/>  </g><\!-- Show outline of canvas using \'rect\' element -->  <rect x=".01cm" y=".01cm" width="4.98cm" height="4.98cm"  fill="none" stroke="blue" stroke-width=".02cm"/></svg>',
      xmlDoc = $.parseXML( xml ),
      $xml = $( xmlDoc ),
      $g = $xml.find( "g" );
      console.log($g);
      

      http://www.w3.org/TR/SVG/struct.html#Groups获取的SVG样本数据

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 2012-06-13
        • 1970-01-01
        • 2015-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多