RSS

Most votes on xml questions 4

Most votes on xml questions 4. #31 SOAP or REST for Web Services? #32 What's the difference between an element and a node in XML? #33 Populate XDocument from String #34 Getting attribute using XPath #35 What to use instead of "addPreferencesFromResource" in a PreferenceActivity? #36 Center a button in a Linear layout #37 XmlSerializer - There was an error reflecting type #38 How can I find an element by CSS class with XPath? #39 How to view AndroidManifest.xml from APK file? #40 Android XML Percent Symbol

Read all the top votes questions and answers in a single page.

#31: SOAP or REST for Web Services? (Score: 388)

Created: 2008-09-16 Last updated: 2019-06-07

Tags: xml, web-services, rest, soap

Is REST a better approach to doing Web Services or is SOAP? Or are they different tools for different problems? Or is it a nuanced issue - that is, is one slightly better in certain arenas than another, etc?

I would especially appreciate information about those concepts and their relation to the PHP-universe and also modern high-end web-applications.

#31 Best answer 1 of SOAP or REST for Web Services? (Score: 562)

Created: 2008-09-16

I built one of the first SOAP servers, including code generation and WSDL generation, from the original spec as it was being developed, when I was working at Hewlett-Packard. I do NOT recommend using SOAP for anything.

The acronym “SOAP” is a lie. It is not Simple, it is not Object-oriented, it defines no Access rules. It is, arguably, a Protocol. It is Don Box’s worst spec ever, and that’s quite a feat, as he’s the man who perpetrated “COM”.

There is nothing useful in SOAP that can’t be done with REST for transport, and JSON, XML, or even plain text for data representation. For transport security, you can use https. For authentication, basic auth. For sessions, there’s cookies. The REST version will be simpler, clearer, run faster, and use less bandwidth.

XML-RPC clearly defines the request, response, and error protocols, and there are good libraries for most languages. However, XML is heavier than you need for many tasks.

#31 Best answer 2 of SOAP or REST for Web Services?(Score: 200)

Created: 2008-09-16

REST is an architecture, SOAP is a protocol.

That’s the first problem.

You can send SOAP envelopes in a REST application.

SOAP itself is actually pretty basic and simple, it’s the WSS-* standards on top of it that make it very complex.

If your consumers are other applications and other servers, there’s a lot of support for the SOAP protocol today, and the basics of moving data is essentially a mouse-click in modern IDEs.

If your consumers are more likely to be RIAs or Ajax clients, you will probably want something simpler than SOAP, and more native to the client (notably JSON).

JSON packets sent over HTTP is not necessarily a REST architecture, it’s just messages to URLs. All perfectly workable, but there are key components to the REST idiom. It is easy to confuse the two however. But just because you’re talking HTTP requests does not necessarily mean you have a REST architecture. You can have a REST application with no HTTP at all (mind, this is rare).

So, if you have servers and consumers that are “comfortable” with SOAP, SOAP and WSS stack can serve you well. If you’re doing more ad hoc things and want to better interface with web browsers, then some lighter protocol over HTTP can work well also.

See also original question in stackoverflow

#32: What's the difference between an element and a node in XML? (Score: 379)

Created: 2008-09-25 Last updated: 2012-12-12

Tags: xml, xmlnode

I’m working in Java with XML and I’m wondering; what’s the difference between an element and a node?

#32 Best answer 1 of What's the difference between an element and a node in XML? (Score: 269)

Created: 2008-09-25

The Node object is the primary data type for the entire DOM.

A node can be an element node, an attribute node, a text node, or any other of the node types explained in the “Node types” chapter.

An XML element is everything from (including) the element’s start tag to (including) the element’s end tag.

#32 Best answer 2 of What's the difference between an element and a node in XML?(Score: 68)

Created: 2008-11-18 Last updated: 2012-12-12

Different W3C specifications define different sets of “Node” types.

Thus, the DOM spec defines the following types of nodes:

The XML Infoset (used by XPath) has a smaller set of nodes:

XPath has the following Node types:

  • root nodes
  • element nodes
  • text nodes
  • attribute nodes
  • namespace nodes
  • processing instruction nodes
  • comment nodes

The answer to your questionWhat is the difference between an element and a node” is:

An element is a type of node. Many other types of nodes exist and serve different purposes.

See also original question in stackoverflow

#33: Populate XDocument from String (Score: 373)

Created: 2009-04-14 Last updated: 2013-03-12

Tags: c#, xml, c#-3.0, linq-to-xml

I’m working on a little something and I am trying to figure out whether I can load an XDocument from a string. XDocument.Load() seems to take the string passed to it as a path to a physical XML file.

I want to try and bypass the step of first having to create the physical XML file and jump straight to populating the XDocument.

Any ideas?

#33 Best answer 1 of Populate XDocument from String (Score: 568)

Created: 2009-04-14 Last updated: 2011-08-08

You can use XDocument.Parse for this.

#33 Best answer 2 of Populate XDocument from String(Score: 83)

Created: 2009-04-14

You can use XDocument.Parse(string) instead of Load(string).

See also original question in stackoverflow

#34: Getting attribute using XPath (Score: 365)

Created: 2010-12-25 Last updated: 2014-05-21

Tags: xml, xpath

Given an XML structure like so:

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>

<book>
  <title lang="eng">Learning XML</title>
  <price>39.95</price>
</book>

</bookstore>

How could I get the value of lang (where lang is eng in book title), for the first element?

#34 Best answer 1 of Getting attribute using XPath (Score: 509)

Created: 2010-12-25 Last updated: 2017-12-11

How could I get the value of lang (where lang=eng in book title), for the first element?

Use:

/*/book[1]/title/@lang

This means:

Select the lang attribute of the title element that is a child of the first book child of the top element of the XML document.

To get just the string value of this attribute use the standard XPath function string():

string(/*/book[1]/title/@lang)

#34 Best answer 2 of Getting attribute using XPath(Score: 53)

Created: 2015-01-24 Last updated: 2015-12-10

Thanks! This solved a similar problem I had with a data attribute inside a Div.

<div id="prop_sample" data-want="data I want">data I do not want</div>

Use this xpath: //*[@id="prop_sample"]/@data-want

Hope this helps someone else!

See also original question in stackoverflow

#35: What to use instead of "addPreferencesFromResource" in a PreferenceActivity? (Score: 363)

Created: 2011-07-25 Last updated: 2017-02-03

Tags: android, xml

I just noticed the fact that the method addPreferencesFromResource(int preferencesResId) is marked deprecated in Android’s documentation (Reference Entry).

Unfortunately, no alternative method is provided in the method’s description.

Which method should be used instead in order to connect a preferenceScreen.xml to the matching PreferenceActivity?

#35 Best answer 1 of What to use instead of "addPreferencesFromResource" in a PreferenceActivity? (Score: 334)

Created: 2011-07-25 Last updated: 2012-12-02

No alternative method is provided in the method’s description because the preferred approach (as of API level 11) is to instantiate PreferenceFragment objects to load your preferences from a resource file. See the sample code here: PreferenceActivity

#35 Best answer 2 of What to use instead of "addPreferencesFromResource" in a PreferenceActivity?(Score: 187)

Created: 2012-11-18

To add more information to the correct answer above, after reading an example from Android-er I found you can easily convert your preference activity into a preference fragment. If you have the following activity:

public class MyPreferenceActivity extends PreferenceActivity
{
    @Override
    protected void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.my_preference_screen);
    }
}

The only changes you have to make is to create an internal fragment class, move the addPreferencesFromResources() into the fragment, and invoke the fragment from the activity, like this:

public class MyPreferenceActivity extends PreferenceActivity
{
    @Override
    protected void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
    }

    public static class MyPreferenceFragment extends PreferenceFragment
    {
        @Override
        public void onCreate(final Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.my_preference_screen);
        }
    }
}

There may be other subtleties to making more complex preferences from fragments; if so, I hope someone notes them here.

See also original question in stackoverflow

#36: Center a button in a Linear layout (Score: 346)

Created: 2009-12-24 Last updated: 2018-03-13

Tags: xml, android, layout

I am using a linear layout to display a pretty light initial screen. It has 1 button that is supposed to centre in the screen both horizontally and vertically. However no matter what I try to do the button will top align centre. I have included the XML below, can some one point me in the right direction?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <ImageButton android:id="@+id/btnFindMe" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:background="@drawable/findme"></ImageButton>

</LinearLayout>

#36 Best answer 1 of Center a button in a Linear layout (Score: 455)

Created: 2012-06-06 Last updated: 2015-12-10

Center using a LinearLayout:

<LinearLayout
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageButton
        android:id="@+id/btnFindMe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/findme" />
</LinearLayout>

#36 Best answer 2 of Center a button in a Linear layout(Score: 382)

Created: 2009-12-24 Last updated: 2009-12-24

If you want to center an item in the middle of the screen don’t use a LinearLayout as these are meant for displaying a number of items in a row.

Use a RelativeLayout instead.

So replace:

android:layout_gravity="center_vertical|center_horizontal"

for the relevant RelativeLayout option:

android:layout_centerInParent="true"

So your layout file will look like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageButton android:id="@+id/btnFindMe" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/findme"></ImageButton>

</RelativeLayout>

See also original question in stackoverflow

#37: XmlSerializer - There was an error reflecting type (Score: 346)

Created: 2008-09-13 Last updated: 2013-09-19

Tags: c#, .net, xml, serialization, .net-2.0

Using C# .NET 2.0, I have a composite data class that does have the [Serializable] attribute on it. I am creating an XMLSerializer class and passing that into the constructor:

XmlSerializer serializer = new XmlSerializer(typeof(DataClass));

I am getting an exception saying:

There was an error reflecting type.

Inside the data class there is another composite object. Does this also need to have the [Serializable] attribute, or by having it on the top object, does it recursively apply it to all objects inside?

#37 Best answer 1 of XmlSerializer - There was an error reflecting type (Score: 422)

Created: 2008-09-13 Last updated: 2020-01-30

Look at the inner exception that you are getting. It will tell you which field/property it is having trouble serializing.

You can exclude fields/properties from xml serialization by decorating them with the [XmlIgnore] attribute.

XmlSerializer does not use the [Serializable] attribute, so I doubt that is the problem.

#37 Best answer 2 of XmlSerializer - There was an error reflecting type(Score: 113)

Created: 2008-09-13

Remember that serialized classes must have default (i.e. parameterless) constructors. If you have no constructor at all, that’s fine; but if you have a constructor with a parameter, you’ll need to add the default one too.

See also original question in stackoverflow

#38: How can I find an element by CSS class with XPath? (Score: 343)

Created: 2009-10-22 Last updated: 2018-05-01

Tags: html, css, xml, xpath

In my webpage, there’s a div with a class named Test.

How can I find it with XPath?

#38 Best answer 1 of How can I find an element by CSS class with XPath? (Score: 546)

Created: 2009-10-22 Last updated: 2016-09-06

This selector should work but will be more efficient if you replace it with your suited markup:

//*[contains(@class, 'Test')]

Or, since we know the sought element is a div:

//div[contains(@class, 'Test')]

But since this will also match cases like class="Testvalue" or class="newTest", @Tomalak’s version provided in the comments is better:

//div[contains(concat(' ', @class, ' '), ' Test ')]

If you wished to be really certain that it will match correctly, you could also use the normalize-space function to clean up stray whitespace characters around the class name (as mentioned by @Terry):

//div[contains(concat(' ', normalize-space(@class), ' '), ' Test ')]

Note that in all these versions, the * should best be replaced by whatever element name you actually wish to match, unless you wish to search each and every element in the document for the given condition.

#38 Best answer 2 of How can I find an element by CSS class with XPath?(Score: 173)

Created: 2014-09-23 Last updated: 2016-07-14

Most easy way..

//div[@class="Test"]

Assuming you want to find <div class="Test"> as described.

See also original question in stackoverflow

#39: How to view AndroidManifest.xml from APK file? (Score: 328)

Created: 2010-11-16 Last updated: 2017-08-19

Tags: android, xml, apk, android-manifest, aapt

Is it possible to view Androidmanifest.xml file?

I just changed the extension of the apk file to zip. This zip file contains the Androidmanifest.xml file. But I am unable view the contents of Androidmanifest.xml. It is fully encrypted.

How can I view the Androidmanifest.xml file?

#39 Best answer 1 of How to view AndroidManifest.xml from APK file? (Score: 229)

Created: 2010-11-16 Last updated: 2017-07-20

Yes you can view XML files of an Android APK file. There is a tool for this: android-apktool

It is a tool for reverse engineering 3rd party, closed, binary Android apps

How to do this on your Windows System:

  1. Download apktool-install-windows-* file
  2. Download apktool-* file
  3. Unpack both to your Windows directory

Now copy the APK file also in that directory and run the following command in your command prompt:

apktool d HelloWorld.apk ./HelloWorld

This will create a directory “HelloWorld” in your current directory. Inside it you can find the AndroidManifest.xml file in decrypted format, and you can also find other XML files inside the "HelloWorld/res/layout" directory.

Here HelloWorld.apk is your Android APK file.

See the below screen shot for more information: alt text

#39 Best answer 2 of How to view AndroidManifest.xml from APK file?(Score: 145)

Created: 2016-10-28

Android Studio can now show this. Go to Build > Analyze APK... and select your apk. Then you can see the content of the AndroidManifset file.

See also original question in stackoverflow

#40: Android XML Percent Symbol (Score: 320)

Created: 2010-12-11 Last updated: 2019-09-10

Tags: android, xml, android-resources, xliff

I have an array of strings in which the % symbol is used. Proper format for using the % is &#37;. When I have a string in that array with multiple &#37; it gives me this error.

 Multiple annotations found at this
 line:
 - error: Multiple substitutions specified in non-positional format;
   did you mean to add the formatted="false" attribute?
 - error: Found tag </item> where </string-array> is expected

#40 Best answer 1 of Android XML Percent Symbol (Score: 482)

Created: 2010-12-11

The Android Asset Packaging Tool (aapt) has become very strict in its latest release and is now used for all Android versions. The aapt-error you’re getting is generated because it no longer allows non-positional format specifiers.

Here are a few ideas how you can include the %-symbol in your resource strings.

If you don’t need any format specifiers or substitutions in your string you can simply make use of the formatted attribute and set it to false:

<string formatted="false">%a + %a == 2%a</string>

In this case the string is not used as a format string for the Formatter so you don’t have to escape your %-symbols. The resulting string is “%a + %a == 2%a”.

If you omit the formatted="false" attribute, the string is used as a format string and you have to escape the %-symbols. This is correctly done with double-%:

<string>%%a + %%a == 2%%a</string>

Now aapt gives you no errors but depending on how you use it, the resulting string can be “%%a + %%a == 2%%a” if a Formatter is invoked without any format arguments:

Resources res = context.getResources();

String s1 = res.getString(R.string.str);
// s1 == "%%a + %%a == 2%%a"

String s2 = res.getString(R.string.str, null);
// s2 == "%a + %a == 2%a"

Without any xml and code it is difficult to say what exactly your problem is but hopefully this helps you understand the mechanisms a little better.

#40 Best answer 2 of Android XML Percent Symbol(Score: 158)

Created: 2011-02-25

To allow the app using formatted strings from resources you should correct your xml. So, for example

<string name="app_name">Your App name, ver.%d</string>

should be replaced with

<string name="app_name">Your App name, ver.%1$d</string>

You can see this for details.

See also original question in stackoverflow


Notes:
  1. This page use API to get the relevant data from stackoverflow community.
  2. Content license on this page is CC BY-SA 3.0.
  3. score = up votes - down votes.