JavaScript How to get attributes from a line of XML in JavaScript? file attributes from command line,modify attributes from command line

I have this line as a part from uml doc.

<children xmi:type="corstr:heighj" uml:name="private" x="255" y="188" width="221" height="51"/>
  • I need the values for x,y width and height *

I tried with this function

function getValues(mainBlock, valueLine)   {


console.log("searching for data in " + valueLine)

console.log("x? " + getValueFromLine(valueLine, "x=\""));
console.log("y? " + getValueFromLine(valueLine, "y=\""));
console.log("width? " + getValueFromLine(valueLine, "width=\""));



function getValueFromLine(valueLine, searchFor)
{
    var position = valueLine.indexOf(searchFor);

    return valueLine.substr(position + searchFor.length, (valueLine.indexOf("\"", position + searchFor.length)) - position + searchFor.length);
}

but apparently something is wrong so I am getting this in the console

x? 255" y="1<br>
index1.html:179 y? 188" width<br>
index1.html:180 width? 221" height="51"/
Answer:1

I'm not sure if there are solutions out there but I've written a function that would help you out.

I've taken the approach of breaking down the strings and parsing the attributes separately, and to allow you to use the elements as per need - I've written a function as a closure to the given string.

It splits on SPACE, then I parsed the breakdowns and split on EQUALS, then parse integers if possible to finally create an object that contains the parsed values. A helper function to get you the specified value is enclosed in the function.

Take a look at the fiddle and let me know if you don't understand the code.

function parse_uml(valueLine) {
    var value_map = {};
    var breakdown = valueLine.split(" ");
    breakdown.shift();
    breakdown[breakdown.length - 1] = breakdown[breakdown.length - 1].substr(0, breakdown[breakdown.length - 1].length - 2);
    breakdown.map(function (i) {
        var attr = i.split("=");
        if (parseInt(attr[1].substr(1, attr[1].length - 2))) {
            value_map[attr[0]] = parseInt(attr[1].substr(1, attr[1].length - 2));
        } else {
            value_map[attr[0]] = attr[1].substr(1, attr[1].length - 2);
        }
    });
    return function (searchFor) {
        return value_map[searchFor];
    };
}

USAGE -

var parsed_uml = parse_uml('<children xmi:type="corstr:heighj" uml:name="private" x="255" y="188" width="221" height="51"/>', 'width');

console.log("Value of x - " + parsed_uml("x"));
console.log("Value of y - " + parsed_uml("y"));
console.log("Value of width - " + parsed_uml("width"));

http://jsfiddle.net/u6yxb4es/

Answer:2

Hi i had written an XML parser that i forked for you. This code converts a dom element to an object. May be the below code helps you to extract the attributes in a given XML tag.

http://jsfiddle.net/wyqm16k0/

 function parsethis(line){
  var x = line.split(" "), dom = x.shift(), obj;
  dom = dom.replace("<","");
  x[x.length-1] = x[x.length-1].replace("/>","");
  x.forEach(function(element, index, array){
    var str = '"'+array[index];
    str = str.replace('=','":');
    array[index] = str;
  });

  obj = x.join("");
  obj = "{"+obj.replace(/""/g,'","')+"}";
  obj = JSON.parse(obj);
  obj.dom = dom;
  return obj;
}

var parsed = parsethis('<children xmi:type="corstr:heighj" uml:name="private" x="255" y="188" width="221" height="51"/>');
console.log("X = "+parsed.x,", Y = "+parsed.y, ", Width = "+parsed.width);
Answer:3

const express = require('express'); const app = express(); app.use('/app', express.static(path.resolve(__dirname, './app'), { maxage: '600s' })) app.listen(9292, function(err){ if (err) ...

const express = require('express'); const app = express(); app.use('/app', express.static(path.resolve(__dirname, './app'), { maxage: '600s' })) app.listen(9292, function(err){ if (err) ...

  1. mvc ignore static files
  2. nginx ignore static files
  3. mvc route ignore static files

How could this conditional statement be simplified? Return statement is being used multiple times. Could for example ternary operators be used in this case? Is returning null a proper way to hide ...

How could this conditional statement be simplified? Return statement is being used multiple times. Could for example ternary operators be used in this case? Is returning null a proper way to hide ...

  1. using ternary operator
  2. using ternary operator javascript
  3. using ternary operator c#
  4. using ternary operator java
  5. using ternary operator python
  6. use ternary operator return
  7. use ternary operator swift
  8. use ternary operator typescript
  9. use ternary operator angularjs
  10. in ternary operators
  11. && in ternary operator javascript
  12. in ternary operator php
  13. && in ternary operator java
  14. in ternary operator c#
  15. using ternary operator in react

After searching a lot on internet when I could not get a solution I am here. I have a requirement in my project which supports multiple languages. When I am switching to a specific language it works ...

After searching a lot on internet when I could not get a solution I am here. I have a requirement in my project which supports multiple languages. When I am switching to a specific language it works ...

In my controller code contain html code in appened form.When i pass the parameters to the onclick function, I didn't get the parameters in the corresponding function. controller foreach ($...

In my controller code contain html code in appened form.When i pass the parameters to the onclick function, I didn't get the parameters in the corresponding function. controller foreach ($...

  1. pass parameters onclick react
  2. pass parameter onclick javascript
  3. pass parameter onclick jquery
  4. pass parameter onclick function
  5. pass parameter onclick event jquery
  6. pass parameter onclick asp.net
  7. pass parameter onclick html
  8. pass parameters onclick
  9. pass parameters onclick android
  10. pass parameter onclick vue
  11. pass parameter onclick angularjs
  12. pass parameter onclick js
  13. pass parameter onclick c#
  14. pass parameter onclick event
  15. passing parameter onclick event javascript
  16. pass parameters to onclick event react
  17. pass multiple parameters onclick
  18. pass parameters jquery onclick function