I made a single scriptfile.html , [with all javascript in file includet] to see the color preview of integer variables from colors.xml from AndroidStudio.
I paste the colors.xml in one << div >> in the scriptfile id="filexml" , parse xml and make sam << div >> with: color | hex | name
Hope it helps: [ EDITed code snipped ]
/* whitespacenowrap _ parent */
.whitespacenowrap_parent {
white-space: nowrap;
/* border:1px solid yellow; */
margin:0px;
padding:0px;
}
/* inlineblock _ child */
.inlineblock_child {
height: 17px;
display: inline-block;
border-top: 1px solid silver;
margin:0px;
padding-left:5px;
}
.width_hex_color{ width: 66px; }
.width_name{width: 473px;
/* border-top:0px; */
}
AndroidStudio colors.xml
copy AndroidStudio colors.xml, to here, in this file, in , and show Colors Hex and Names, dont need first line
show_colors_xml_Android_Studio
temp_test_div_id
temp_test_div_id
#545456
#545456
#fbd7bd
#ff0bff
#3700B3
#6200EE
#03DAC5
#ff0eff
#03DAC5
#addac5
#33C458
#30D158
#4e5850
#004953
#5AC8FA
#64D2FF
#5EB0E5
#0A84FF
#007AFF
#5E5CE6
#5856D6
#0040DD
#0071A4
#010f8a
#3634A3
#3700B3
#8944AB
#8944AB
#BF5AF2
#AF52DE
#FF6482
#FF453A
#FF375F
#FF3B30
#FF375F
#D30F45
#BA0C2E
#52514f
#52514f
#6C6C70
#8E8E93
#AEAEB2
#C7C7CC
#D1D1D6
#E5E5EA
#EBEBF0
#F2F2F7
#F9F6EF
#ebebef
#ebebe3
#E4E4E2
#98989D
#8E8E93
#6C6C70
#545456
#48484A
#3A3A3C
#2C2C2E
#1C1C1E
#242426
#fbd7bd
#E6C7C2
#dbc1ef
#FFCC00
#FFD60A
#de9e95
#b69221
#FF9500
#FF9500
var filexml_is_empty=0;
// on buttn click, or window.onload fires when the entire page loads (images, styles, etc.) :
//
window.onload =
function show_colors_xml_Android_Studio() {
if(filexml_is_empty)return;
var debug=false; // console help search: if(debug)
var str = document.getElementById("filexml").innerHTML;
document.getElementById("temp_test_div_id").innerHTML="";
document.getElementById("filexml").innerHTML="";
filexml_is_empty=1;
//clean string str because error in xmlToJson.parse()
// console.log("String ori: ", str);
strH = str.toHtmlEntitiesRemoveBadXMLchars();
// console.log("str toEntities: ", str);
str = String.fromHtmlEntities(strH);
// String is class obj ?
// console.log("clean from htmlentities:", String.fromHtmlEntities(str));
//
// strip xml header because error in strg xml to json xmlToJson.parse()
str = str.match(/\/);
// document.getElementById("outprn3").innerHTML = str;
// console.log("match str obj:", str);
// regex match in first str[0]
str=str[0];
var json = xmlToJson.parse( str );
// console.log("json: ", json);
/***/
if(debug){
document.getElementById("outprn3").innerHTML='
strH.toHtmlEntities:
'+ strH;var jsonstr = JSON.stringify(json);
jsonstr=jsonstr.replace(/\[/gm,",[
");
jsonstr=jsonstr.replace(/\]/gm,",
]
");
jsonstr=jsonstr.replace(/,/gm,",
");
document.getElementById("outprn4").innerHTML='
jsonstr:
'+ jsonstr;console.log("match parse:", jsonstr);
console.log("str:", str);
console.log("json[0]", json[0]); // erster char!!!!!
console.log('json["resources"]: ', json["resources"] );//color: array 3
console.log("json.resources: ", json.resources );
console.log("json.resources.color: ", json.resources.color );
console.log("json.resources.color[0]: ", json.resources.color[0] );
console.log("json.resources.color[0].name: ", json.resources.color[0].name );
console.log("json.resources.color[0].text: ", json.resources.color[0].text );
console.log("json.resources.color[1].name: ", json.resources.color[1].name );
console.log("json.resources.color[1].text: ", json.resources.color[1].text );
console.log("json.resources.color[2].name: ", json.resources.color[2].name );
console.log("json.resources.color[2].text: ", json.resources.color[2].text );
//console.log("json.resources.color[0][0]: ", json.resources.color[0][0] ); //undef
//console.log("json.resources.color.name: ", json.resources.color.name ); //undef
}
/***/
console.log('Object.keys(json).length: ', Object.keys(json.resources.color).length )// 3
document.getElementById("outprn").innerHTML=' Object.keys(json).length: '+ Object.keys(json.resources.color).length +'';
var cdiv = document.getElementById("color_div").innerHTML;
n=Object.keys(json.resources.color).length;
// test XML problem:
// n=n+2; //ok, til error, todo: error check
for(let i=0; i
cdiv += color_num(i, json);
// ok function til ERROR and show htm til error
document.getElementById("color_div").innerHTML = cdiv;
}
}
// ver_2 color name text move to right
function color_num(n, json){
var color_div ='
/* add line number 100+n /***/
color_div+='
color_div+=1001+n;
color_div+='
/***/
color_div+='
color_div+=json.resources.color[ n ].text;
color_div+='
color_div+=b6=json.resources.color[ n ].name;
color_div+='
return color_div;
}
/**
* Convert a string to HTML entities , ORIGINAL
*/
String.prototype.toHtmlEntities = function() {
return this.replace(/[\s\S]/gm, function(s) {
// return this.replace(/./gm, function(s) {
// \s Find a whitespace character
// \S Find a non-whitespace character
// return "" + s.charCodeAt(0) + ";";
return (s.match(/[a-z0-9\s]+/i)) ? s : "" + s.charCodeAt(0) + ";";
});
};
// add strip bad xml chars
// Where he links to this article: http://www.w3.org/TR/xml/#charsets
// Basically, all characters below 0x20 is disallowed,
// except 0x9 (TAB), 0xA (CR?), 0xD (LF?)
String.prototype.toHtmlEntitiesRemoveBadXMLchars = function() {
// \s Find a whitespace character
// \S Find a non-whitespace character [\s\S]
//return this.replace(/./gm, function(s) { // [\s\S]
return this.replace(/[\s\S]/gm, function(s) {
// return "" + s.charCodeAt(0) + ";";
//add
var q=s.charCodeAt(0);
return (s.match(/[a-z0-9\s]+/i)) ? s :
s=
(
q== 0x9 || q== 0xA || q== 0xD ||
((q>= 0x20) && (q<= 0xD7FF)) ||
((q>= 0xE000) && (q<= 0xFFFD)) ||
((q>= 0x10000) && (q<= 0x10FFFF ))
)?
"" + s.charCodeAt(0) + ";"
:
""
;
});
};
/**
* Create string from HTML entities
*/
String.fromHtmlEntities = function(string) {
return (string+"").replace(/\d+;/gm,function(s) {
return String.fromCharCode(s.match(/\d+/gm)[0]);
})
};
/**
*/
/**
* Object assign is required, so ensure that browsers know how to execute this method
*
* @method Object.assign
* @returns {Function}
*/
if (typeof Object.assign != 'function') {
// Must be writable: true, enumerable: false, configurable: true
Object.defineProperty(Object, "assign", {
value: function assign(target, varArgs) { // .length of function is 2
'use strict';
if (target == null) { // TypeError if undefined or null
throw new TypeError('Cannot convert undefined or null to object');
}
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
if (nextSource != null) { // Skip over if undefined or null
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
},
writable: true,
configurable: true
});
}
/**
* Object to convert XML into a structured JSON object
*
* @method xmlToJson
* @returns {Object}
*/
var xmlToJson = (function () {
var self = this;
/**
* Adds an object value to a parent object
*
* @method addToParent
* @param {Object} parent
* @param {String} nodeName
* @param {Mixed} obj
* @returns none
*/
self.addToParent = function (parent, nodeName, obj) {
// If this is the first or only instance of the node name, assign it as
// an object on the parent.
if (!parent[nodeName]) {
parent[nodeName] = obj;
}
// Else the parent knows about other nodes of the same name
else {
// If the parent has a property with the node name, but it is not an array,
// store the contents of that property, convert the property to an array, and
// assign what was formerly an object on the parent to the first member of the
// array
if (!Array.isArray(parent[nodeName])) {
var tmp = parent[nodeName];
parent[nodeName] = [];
parent[nodeName].push(tmp);
}
// Push the current object to the collection
parent[nodeName].push(obj);
}
};
self.convertXMLStringToDoc = function (str) {
var xmlDoc = null;
if (str && typeof str === 'string') {
// Create a DOMParser
var parser = new DOMParser();
// Use it to turn your xmlString into an XMLDocument
xmlDoc = parser.parseFromString(str, 'application/xml');
}
return xmlDoc;
}
/**
* Validates if an data is an XMLDocument
*
* @method isXML
* @param {Mixed} data
* @returns {Boolean}
*/
self.isXML = function (data) {
var documentElement = (data ? data.ownerDocument || data : 0).documentElement;
return documentElement ? documentElement.nodeName.toLowerCase() !== 'html' : false;
};
/**
* Reads through a node's attributes and assigns the values to a new object
*
* @method parseAttributes
* @param {XMLNode} node
* @returns {Object}
*/
self.parseAttributes = function (node) {
var attributes = node.attributes,
obj = {};
// If the node has attributes, assign the new object properties
// corresponding to each attribute
if (node.hasAttributes()) {
for (var i = 0; i < attributes.length; i++) {
obj[attributes[i].name] = self.parseValue(attributes[i].value);
}
}
// return the new object
return obj;
};
/**
* Rips through child nodes and parses them
*
* @method parseChildren
* @param {Object} parent
* @param {XMLNodeMap} childNodes
* @returns none
*/
self.parseChildren = function (parent, childNodes) {
// If there are child nodes...
if (childNodes.length > 0) {
// Loop over all the child nodes
for (var i = 0; i < childNodes.length; i++) {
// If the child node is a XMLNode, parse the node
if (childNodes[i].nodeType == 1) {
self.parseNode(parent, childNodes[i]);
}
}
}
};
/**
* Converts a node into an object with properties
*
* @method parseNode
* @param {Object} parent
* @param {XMLNode} node
* @returns {Object}
*/
self.parseNode = function (parent, node) {
var nodeName = node.nodeName,
obj = Object.assign({}, self.parseAttributes(node)),
tmp = null;
// If there is only one text child node, there is no need to process the children
if (node.childNodes.length == 1 && node.childNodes[0].nodeType == 3) {
// If the node has attributes, then the object will already have properties.
// Add a new property 'text' with the value of the text content
if (node.hasAttributes()) {
obj['text'] = self.parseValue(node.childNodes[0].nodeValue);
}
// If there are no attributes, then the parent[nodeName] property value is
// simply the interpreted textual content
else {
obj = self.parseValue(node.childNodes[0].nodeValue);
}
}
// Otherwise, there are child XMLNode elements, so process them
else {
self.parseChildren(obj, node.childNodes);
}
// Once the object has been processed, add it to the parent
self.addToParent(parent, nodeName, obj)
// Return the parent
return parent;
};
/**
* Interprets a value and converts it to Boolean, Number or String based on content
*
* @method parseValue
* @param {Mixed} val
* @returns {Mixed}
*/
this.parseValue = function (val) {
// Create a numeric value from the passed parameter
var num = Number(val);
// If the value is 'true' or 'false', parse it as a Boolean and return it
if (val.toLowerCase() === 'true' || val.toLowerCase() === 'false') {
return (val.toLowerCase() == 'true');
}
// If the num parsed to a Number, return the numeric value
// Else if the valuse passed has no length (an attribute without value) return null,
// Else return the param as is
return (isNaN(num)) ? val.trim() : (val.length == 0) ? null : num;
};
// Expose the API
return {
parse: function (xml) {
if (xml && typeof xml === 'string') {
xml = self.convertXMLStringToDoc(xml);
}
return (xml && self.isXML(xml)) ? self.parseNode({}, xml.firstChild) : null;
}
}
})();