Using Javascript to convert an SVG file into DOM elements

Seeking assistance with uploading an SVG file and then inspecting or parsing it to utilize the elements and values within the DOM. After extensive searches, I've only found information on parsing from the DOM itself.

Is this task feasible? If so, could you provide guidance in the right direction?

Thank you for your help.

CLARIFY:

To clarify, I require someone to upload an SVG file, after which my function would parse it and extract only the path data for specific element usage. I am working with AngularJS and Javascript.

Answer №1

To access and manipulate SVG markup, you can treat it like any other XML file. If you need a javascript solution,

For example, in the javascript code snippet provided, the variable sMyString contains the SVG content after parsing it using DOMParser.

var sMyString = "<a id=\"a\"><b id=\"b\">hey!<\/b><\/a>";
var oParser = new DOMParser();
var oDOM = oParser.parseFromString(sMyString, "image/svg+xml");
// print the name of the root element or error message
console.log(oDOM.documentElement.nodeName == "parsererror" ? "Error while parsing" : oDOM.documentElement.nodeName);

When using PHP with SimpleXML, like in the following snippet, you can handle SVG content stored in $xmlstr:

$movies = new SimpleXMLElement($xmlstr);
echo $movies->movie[0]->plot;

Below is a complete PHP example of extracting SVG data:

<?php
$xmlstr = '...'; // SVG content goes here
$xml = new SimpleXMLElement($xmlstr);

print '<pre>';

// Find and display relevant path data (example shown is specific to provided SVG content)
var_dump($xml->symbol->g->path);
print '<hr>';
print $xml->symbol->g->path['d'];
print '</pre>';

?>

If you want to see a JavaScript approach, check out my fiddle: https://jsfiddle.net/tobiasbeuving/gyheqvfs/5/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to extract cookie value in a node.js application using socket.io

How can I retrieve cookie values in a node server using socket.io? Whenever a socket sends a message to the server, I need to check the value of the cookie. However, I only want to obtain the cookie value and not the session cookie as done in Express. r ...

Exploring Vue's data binding with both familiar and mysterious input values

How can I model an object in Vue that contains both known and unknown values? The quantity is unknown, but the type is known. I need to present user inputs for each type, where the user enters the quantity. How should I approach this? data() { retur ...

Printing long contents on Chrome's ion-modal-view is not supported on every page

I have tried various solutions found in articles but have not been successful. Here is an example of one such attempt: @media print { .modal { position: absolute; left: 0; top: 0; margin: 0; padding: 0; visibility: visible; / ...

What is the process to set a Woocommerce checkout field as optional when a checkbox is marked?

I need assistance with customizing the checkout page on Wordpress Woocommerce. Specifically, I want to make the field 'billing_name' not required if the checkbox 'buy_on_company' is checked. Currently, I have successfully hidden ' ...

Removing a select menu in Discord.js once a selection has been made

Currently in the process of creating a ticket tool that utilizes a select menu for choosing a topic const row = new MessageActionRow() .addComponents( new MessageSelectMenu() .setCustomId(`select_menu`) .setPlac ...

Understanding the npm install command

I'm trying to understand how npm install actually functions. Shouldn't it have automatically installed all the dependencies listed in package.json? I visited the documentation npm install npm install (in package directory, no arguments): ...

What is the best way to play a video from a certain time point in a React application?

How can I make my component automatically play from a specific time like 00:07:12,600 instead of starting from the beginning? import style from './Hero.module.css'; import Image from 'next/image'; import ReactPlayer from 'react-pla ...

Similar to the filter() method in Vanilla Javascript but behaves in a equivalent way to

Looking to convert some JQuery code to Vanilla JS. $('#myID :not(a, span)').contents().filter( function() { return this.nodeType === 3 && this.data.trim().length > 0;}) .wrap('<span class="mySpanClass" />'); I&a ...

Developing a notification system using a combination of ajax, jquery, and Iframe

I am in the process of setting up a messaging system on my website. Currently, I have a table with three columns - two integer fields (from and to) and a timestamp for the date of sending. On one section of the page, I want to display a list of messages ...

"Utilizing Vuex: Fetch data from API when the first getter is accessed, and subsequently retrieve it from the local

I have a Vuex instance that is responsible for fetching data from an API. The initial access to the store should trigger a call to load the data from the API. Subsequent accesses should return the previously loaded data stored in store.empresas. This is th ...

Place a <script> tag within the Vue template

I am currently developing an integration with a payment service. The payment service has provided me with a form that includes a script tag. I would like to insert this form, including the script tag, into my component template. However, Vue does not allo ...

Angular Bootstrap Calendar: Creating a Unique Custom Event Type

Check out this link If you look at the examples page here, you'll see that the event types are listed as INFO, WARNING, and more. Can I customize these event types? I want them to be able to change dynamically. ...

Is it possible to use a Jasmine spy on a fresh instance?

In need of assistance with testing a TypeScript method (eventually testing the actual JavaScript) that I'm having trouble with. The method is quite straightforward: private static myMethod(foo: IFoo): void { let anInterestingThing = new Interesti ...

PHP code to paginate and format content for Point of Sale printers

Currently, I am working on a project that requires the use of a POS printer to generate receipts. The client has recently requested a new feature where the paper will be automatically cut after the receipt is printed. This way, if the client needs to pri ...

Issues with Content-Security-Policy Implementation in Next JS and AMP Technology

While developing an AMP application with Next JS, everything seems to work perfectly in localhost. However, upon moving to the production environment, I encountered errors related to AMP not being allowed to load its workers. The initial error message is: ...

AngularJS: The delay in updating variable bindings following a REST API call

When utilizing a REST service, I wanted to implement a variable to show whether the service is currently loading or not. Controller $scope.loading = true; $http.get('/Something'). success(function(data, status, headers, config) ...

Bringing in Vue from 'vue' allows for the incorporation of 'different' Vue to diverse documents

This issue involves the interaction between Webpack, ES6 import syntax, and Vue. Currently, I am working on a Vuex mutation that is responsible for adding a new key-value pair mykey: [] to an object within the state. To ensure reactivity, I need to use Vu ...

How can we best organize a VueJS application to accommodate this specific logic?

I am currently working on an app that needs to display data fetched from multiple sources based on a condition. The issue I am facing is that the process of fetching, organizing, and returning the data varies depending on the source, sometimes even requiri ...

JavaScript struggles to obtain the timezone information when Daylight Saving Time is

Can someone help me with setting a user's timezone offset for PHP through ajax? When a page is loaded with session data, if there is no pre-existing data, the following script is inserted into the page: <script type="text/javascript"> $(doc ...

Pass the DateTime object from ASP.NET to angularjs as a structured data type instead of converting it to a

I'm encountering an issue where I am sending a complex object from my ASP.NET backend to the AngularJS frontend. This object includes a DateTime property and a list of objects, each with their own DateTime property. These properties are all sent as st ...