[英]Load a svg file(xml) and extract specific information using javascript
我正在嘗試使用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角色名稱,'node value'Actor角色
議決-期部分
$.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現在正確了
這就是我一直在加載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');
});
我將使用以下代碼: Ajax或JavaScript:根據服務器響應更改樣式
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來提取屬性。
從文檔中: 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);
SVG示例數據可從http://www.w3.org/TR/SVG/struct.html#Groups獲得
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.