Convert XML to HTML JS [duplicate] - javascript

This question already has an answer here:
Convert XML to HTML using jquery/javascript
(1 answer)
Closed 2 years ago.
I am a total 0 in JS but I got a task to write a simple function of converting XML to HTML
Example:
<Translation code="200" lang="de-en">
<text>Cat</text>
</Translation>
'Cat' should be in HTML
XML structure doesn't change but the content of text node does
Yeah I can google couple of hours how to get it or ask here and close the task, appreciate any help

The quickest is using jQuery
const xml = `<Translation code="200" lang="de-en">
<text>Cat</text>
</Translation>`
$("#content").text($(xml).find("text").text())
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="content"></div>

Related

I'm using javascript to scrape data. I want to scrape just html-tags [duplicate]

This question already has answers here:
Parse an HTML string with JS
(15 answers)
Remove all text content from HTML div but keep HTML tags and structure
(8 answers)
Get the string representation of a DOM node
(13 answers)
I need help using recursion to navigate every element in the DOM
(2 answers)
Inserting arbitrary HTML into a DocumentFragment
(11 answers)
Closed 26 days ago.
I'm using javascript to scrape data. I want to scrape just html-tags and their attributes (everything between <> )without content.
like for example given:
<h1>My First Heading</h1>
<div>
<p>My first paragraph.</p>
</div>
I would extract the following:
<h1> </h1>
<div>
<p> </p>
</div>
Any way to do that?
Thanks in advance!
Regular expression looks but this solution sounds naive.

read file content on form input in angularjs [duplicate]

This question already has answers here:
ng-model for `<input type="file"/>` (with directive DEMO)
(13 answers)
Closed 4 years ago.
I have a JSON file in my HDD, how can I read the content of this file when I select it with input field?
I have to read the content and send content as data in some API!
I need something like this :
<input type="file" id="dashboardExportedFile">
<button type="button" ng-click="importDashboard">Import</button>
<script>
$scope.importDashboard = function (
var content = $("#dashboardExportedFile").content
console.log(content);
};
</script>
I think you need a Filereader. Follow my Link to see an working example with .txt File. Hope u can use it:
[JSFiddle][1]
[1]: http://jsfiddle.net/alexsuch/6aG4x/

Injecting <br/> tag form a Json file is not working on an Angular page [duplicate]

This question already has answers here:
Angular HTML binding
(24 answers)
Closed 4 years ago.
I am trying to get the Line Break element working on my Angular page, injecting it form my Json file, with not luck.
I had a look to the following question: SO similar question but I think I have got a different scenario.
My scenario:
JSON FILE
{
"boxLeft": {
"boxLeftHeaderDesktop": "HOURS PER <br/> WEEK",
}
}
ANGULAR:
Angular version: 1.4.9
<div class="header">
{{boxLeft.boxLeftHeaderDesktop}}
</div>`
RESULT - rong
HOURS PER <br/> WEEK
WHAT I HAVE TRIED:
1: {{locale.earnings.boxLeftHeaderDesktop | raw}} - not working
2: <br \/> - not working
3: <div class="header">
<div [innerHTML]="boxLeft.boxLeftHeaderDesktop"></div>
</div> - not working
Please, is there any other solution which I could try to use?
HOW I HAVE SOLVED IT:
using: angular-sanitize.js
use the innerHTML to render the html tags
<div class="header">
<div [innerHTML]="boxLef.boxLeftHeaderDesktop"></div>
</div>

How to get all the html inside a tag as it is using jquery? [duplicate]

This question already has answers here:
What's the right way to decode a string that has special HTML entities in it? [duplicate]
(7 answers)
Closed 6 years ago.
Consider this for example:
<div>
A&
<br>
B
</div>
If I use $('div').html(), it returns A&<br>B. It converts A& into A& which I do not want.
If use use $('div').text(), it returns A&B. It ignores the <br> which I do not want either.
I'm looking for a way to get all the html inside the div without parsing it and without skipping over the tags like <br> either.
This is the result I want: A&<br>B. How do I achieve that?
function decodeEntities(encodedString) {
var textArea = document.createElement('textarea');
textArea.innerHTML = encodedString;
return textArea.value;
}
console.log(decodeEntities($('div').first().html()))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
A&
<br>
B
</div>
EDIT
See also node.innerText
https://developer.mozilla.org/en-US/docs/Web/API/Node/innerText
This is really a "hack" but it does work.
$("<textarea>").html($("div").html()).val()

Save a javascript object in an HTML object [duplicate]

This question already has answers here:
Can I add a custom attribute to an HTML tag?
(18 answers)
Closed 3 years ago.
Is there any way to save a javascript tag in an HTML object? What I would like to do is something like this:
<div id="htmlTag">some text</div>
<script type="text/javascript">
htmlTag.something = {s:"a string",n:1,b:false,a:[3,2,1],f:function(){alert("hello")}}
</script>
I think your only problem is you're not getting the node.
document.getElementById("htmlObject").something = {hi: 1};
You can convert JS objects to JSON string with this plugin, if that is what you want to achieve.
var json_data = JSON.stringify(yourObj);
You can do this using the javascript function JSON.stringify()
Javascript
<div id="htmlTag">some text</div>
<script type="text/javascript">
document.getElementById('htmlTag').something = JSON.stringify({s:"a string",n:1,b:false,a:[3,2,1],f:function(){alert("hello")}});
</script>
Here is a JSFiddle : https://jsfiddle.net/LcrLeg8a/1/

Categories