Posts

Showing posts with the label Xml

Android Spinner Dropdown Arrow Not Displaying

Answer : This works for me, much simpler as well: <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Light" android:spinnerMode="dropdown" /> And in your class file: spinner = (Spinner) view.findViewById(R.id.spinner); ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.spinner_data, android.R.layout.simple_spinner_dropdown_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); Hope this helps ;) Try this one: <Spinner android:id="@+id/spinnPhoneTypes" android:layout_width="0dp" style="@android:style/Widget.Spinner.DropDown" android:layout_height="@dimen/thirtyFive" android:layout_marginLeft="10dp" android:layout_weight="1...

"Content Is Not Allowed In Prolog" When Parsing Perfectly Valid XML On GAE

Answer : The encoding in your XML and XSD (or DTD) are different. XML file header: <?xml version='1.0' encoding='utf-8'?> XSD file header: <?xml version='1.0' encoding='utf-16'?> Another possible scenario that causes this is when anything comes before the XML document type declaration. i.e you might have something like this in the buffer: helloworld<?xml version="1.0" encoding="utf-8"?> or even a space or special character. There are some special characters called byte order markers that could be in the buffer. Before passing the buffer to the Parser do this... String xml = "<?xml ..."; xml = xml.trim().replaceFirst("^([\\W]+)<","<"); This error message is always caused by the invalid XML content in the beginning element. For example, extra small dot “.” in the beginning of XML element. Any characters before the “ <?xml…. ” will cause above “ org.xml.sax.SAXParseExcept...

Convert XML To JSON (and Back) Using Javascript

Answer : I think this is the best one: Converting between XML and JSON Be sure to read the accompanying article on the xml.com O'Reilly site, which goes into details of the problems with these conversions, which I think you will find enlightening. The fact that O'Reilly is hosting the article should indicate that Stefan's solution has merit. https://github.com/abdmob/x2js - my own library (updated URL from http://code.google.com/p/x2js/): This library provides XML to JSON (JavaScript Objects) and vice versa javascript conversion functions. The library is very small and doesn't require any other additional libraries. API functions new X2JS() - to create your instance to access all library functionality. Also you could specify optional configuration options here X2JS.xml2json - Convert XML specified as DOM Object to JSON X2JS.json2xml - Convert JSON to XML DOM Object X2JS.xml_str2json - Convert XML specified as string to JSON X2JS.json2xml_str - Convert JSON to XML s...

Create XML In Javascript

Answer : Disclaimer: The following answer assumes that you are using the JavaScript environment of a web browser. JavaScript handles XML with 'XML DOM objects'. You can obtain such an object in three ways: 1. Creating a new XML DOM object var xmlDoc = document.implementation.createDocument(null, "books"); The first argument can contain the namespace URI of the document to be created, if the document belongs to one. Source: https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createDocument 2. Fetching an XML file with XMLHttpRequest var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (xhttp.readyState == 4 && xhttp.status == 200) { var xmlDoc = xhttp.responseXML; //important to use responseXML here } xhttp.open("GET", "books.xml", true); xhttp.send(); 3. Parsing a string containing serialized XML var xmlString = "<root></root>"; var parser = new DOMParser(); var xmlDoc = pa...

Android Item Size In Layer-list

Answer : I found solution, but I expected better way. Here is final code: <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <gradient android:endColor="#000004" android:gradientRadius="1000" android:startColor="#1f3371" android:type="radial" > </gradient> </shape> </item> <item> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/texture_5" android:tileMode="repeat" /> </item> <item android:drawable="@drawable/logo" android:bottom="214dp" android:top="214dp" ...

Convert XML File To Csv File Format In C#

Answer : using System.IO; using System.Xml.Serialization; You can do like this: public class Sequence { public Point[] SourcePath { get; set; } } using (FileStream fs = new FileStream(@"D:\youXMLFile.xml", FileMode.Open)) { XmlSerializer serializer = new XmlSerializer(typeof(Sequence[])); var data=(Sequence[]) serializer.Deserialize(fs); List<string> list = new List<string>(); foreach(var item in data) { List<string> ss = new List<string>(); foreach (var point in item.SourcePath) ss.Add(point.X + "," + point.Y); list.Add(string.Join(",", ss)); } File.WriteAllLines("D:\\csvFile.csv", list); } In an alternate way you can use leverage the power of XSLT to convert it, Steps Create an Xml stylesheet to convert xml to csv Use XslCompiledTransform() to convert get the csv string save the csv string to a file You may came up with an Xslt like this, call it data.xsl <?...

Access #text Property Of XMLAttribute In Powershell

Answer : Besides #text , you can also access XmlAttribute 's value via Value property : $attr = $xml.SelectSingleNode("//obj/indexlist/index[@name='DATE']/@value") #print old value $attr.Value #update attribute value $attr.Value = "new value" #print new value $attr.Value Note that Value in $attr.Value is property name of XmlAttribute . It doesn't affected by the fact that the attribute in your XML named value . Don't select the attribute, select the node. The attributes of the node will be represented as properties and can be modified as such: $node = $xml.SelectSingleNode("//obj/indexlist/index[@name='DATE']") $node.value = 'foo' Use a loop if you need to modify several nodes: $nodes = $xml.SelectNodes("//obj/indexlist/index[@name='DATE']") foreach ($node in $nodes) { $node.value = 'foo' }

Android - Resource Linking Failed / Failed Linking References

Image
Answer : Solution 1: Set your compileSdkVersion to 28 and let Android Studio download the needed files. If you already targetting this version, you could try cleaning your project and sync your gradle files. In my case, I made two custom backgrounds which were not recognised. I removed the <?xml version="1.0" encoding="utf-8"?> tag from the top of those two XML resources file. This worked for me, after trying many solutions from the community. Errors with XML files are quite hard to figure out. They even trickle their impact down to Java files.

Android Bitmap Image Size In XML

Answer : Only API 23 or later Just use <item android:width="200dp" android:height="200dp" android:drawable="@drawable/splash_background" android:gravity="center" /> instead <item android:left="200dp" android:right="200dp"> <bitmap android:src="@drawable/splash_background" android:gravity="center" /> </item> Although there is no width and height parameters for bitmap, you can set bitmap size using gravity and item width/height. <item android:width="230dp" android:height="70dp"> <bitmap android:gravity="fill_horizontal|fill_vertical" android:src="@drawable/screen" /> </item> You can set item width and height and scale bitmap inside of it using gravity. I know this is old question but maybe someone will find it useful. WARNING: As already ...

Android Shape: Circle With Cross(plus)

Answer : I accomplished something similar (a solid circle with a white plus in the middle) using this drawable xml: <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="oval"> <solid android:color="@color/accent"/> </shape> </item> <item> <shape android:shape="line"> <stroke android:width="5dp" android:color="@android:color/white" /> </shape> </item> <item> <rotate android:fromDegrees="90" android:pivotX="50%" android:pivotY="50%" android:toDegrees="-90"> <shape android:shape="line"> <stroke android:width="5dp...

Converting XML To JSON Using Python?

Answer : xmltodict (full disclosure: I wrote it) can help you convert your XML to a dict+list+string structure, following this "standard". It is Expat-based, so it's very fast and doesn't need to load the whole XML tree in memory. Once you have that data structure, you can serialize it to JSON: import xmltodict, json o = xmltodict.parse('<e> <a>text</a> <a>text</a> </e>') json.dumps(o) # '{"e": {"a": ["text", "text"]}}' There is no "one-to-one" mapping between XML and JSON, so converting one to the other necessarily requires some understanding of what you want to do with the results. That being said, Python's standard library has several modules for parsing XML (including DOM, SAX, and ElementTree). As of Python 2.6, support for converting Python data structures to and from JSON is included in the json module. So the infrastructure is there. You can use the ...

Convert Python ElementTree To String

Answer : Element objects have no .getroot() method. Drop that call, and the .tostring() call works: xmlstr = ElementTree.tostring(et, encoding='utf8', method='xml') You only need to use .getroot() if you have an ElementTree instance. Other notes: This produces a bytestring , which in Python 3 is the bytes type. If you must have a str object, you have two options: Decode the resulting bytes value, from UTF-8: xmlstr.decode("utf8") Use encoding='unicode' ; this avoids an encode / decode cycle: xmlstr = ElementTree.tostring(et, encoding='unicode', method='xml') If you wanted the UTF-8 encoded bytestring value or are using Python 2, take into account that ElementTree doesn't properly detect utf8 as the standard XML encoding, so it'll add a <?xml version='1.0' encoding='utf8'?> declaration. Use utf-8 or UTF-8 (with a dash) if you want to prevent this. When using encoding="unicode" no dec...

Converting Xml To Dictionary Using ElementTree

Answer : The following XML-to-Python-dict snippet parses entities as well as attributes following this XML-to-JSON "specification": from collections import defaultdict def etree_to_dict(t): d = {t.tag: {} if t.attrib else None} children = list(t) if children: dd = defaultdict(list) for dc in map(etree_to_dict, children): for k, v in dc.items(): dd[k].append(v) d = {t.tag: {k: v[0] if len(v) == 1 else v for k, v in dd.items()}} if t.attrib: d[t.tag].update(('@' + k, v) for k, v in t.attrib.items()) if t.text: text = t.text.strip() if children or t.attrib: if text: d[t.tag]['#text'] = text else: d[t.tag] = text return d It is used: from xml.etree import cElementTree as ET e = ET.XML(''' <root> <e /> <e>text</e> <e name="va...

Convert XML To JSON With NodeJS

Answer : I've used xml-js - npm to get the desired result. First of all I've installed xml-js via npm install xml-js Then used the below code to get the output in json format var convert = require('xml-js'); var xml = require('fs').readFileSync('./testscenario.xml', 'utf8'); var result = convert.xml2json(xml, {compact: true, spaces: 4}); console.log(result); You can use xml2json npm for converting your xml in to json. xml2json. Step 1:- Install package in you project npm install xml2json Step 2:- You can use that package and convert your xml to json let xmlParser = require('xml2json'); let xmlString = `<?xml version="1.0" encoding="UTF-8"?> <TestScenario> <TestSuite name="TS_EdgeHome"> <TestCaseName name="tc_Login">dt_EdgeCaseHome,dt_EdgeCaseRoute</TestCaseName> <TestCaseName name="tc_Logout">dt_EdgeCaseRoute</TestCaseName> ...

Android: TextColor Of Disabled Button In Selector Not Showing?

Answer : You need to also create a ColorStateList for text colors identifying different states. Do the following: Create another XML file in res\color named something like text_color.xml . <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- disabled state --> <item android:state_enabled="false" android:color="#9D9FA2" /> <item android:color="#000"/> </selector> In your style.xml , put a reference to that text_color.xml file as follows: <style name="buttonStyle" parent="@android:style/Widget.Button"> <item name="android:textStyle">bold</item> <item name="android:textColor">@color/text_color</item> <item name="android:textSize">18sp</item> </style> This should resolve your issue. 1.Create a color folder ...