Ошибка not well formed invalid token

I am working on an application, with following XML. but whenI try to clean/build my project the following error occurs:

«error: Error parsing XML: not well-formed (invalid token)»

<?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"
>
<TextView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello";
/>
</LinearLayout>

What does it possibly mean?

Sree's user avatar

Sree

3,1262 gold badges31 silver badges39 bronze badges

asked Aug 17, 2011 at 8:19

Usama Sarwar's user avatar

Usama SarwarUsama Sarwar

8,8727 gold badges53 silver badges79 bronze badges

1

I had this problem, and when I had android:text="< Go back" it had the correct syntax highlighting, but then I realized it’s the < symbol that is messing everything up.

answered Jan 23, 2016 at 2:18

Rock Lee's user avatar

3

It means there is a compilation error in your XML file, something that shouldn’t be there: a spelling mistake/a spurious character/an incorrect namespace.

Your issue is you’ve got a semicolon that shouldn’t be there after this line:

  android:text="@string/hello";

answered Aug 17, 2011 at 8:21

Blundell's user avatar

BlundellBlundell

75.5k30 gold badges208 silver badges233 bronze badges

3

I had same problem. you can’t use left < arrow in text property like as android:text="< Go back" in your xml file. Remove any < arrow from you xml code.

Hope It will helps you.

answered Dec 29, 2016 at 6:17

Dalvinder Singh's user avatar

Dalvinder SinghDalvinder Singh

1,0631 gold badge12 silver badges19 bronze badges

1

Verify that you don’t have any spaces or tabs before

<?xml version="1.0" encoding="utf-8"?>

also refresh and clean your project in eclipse.

I get this error every now and then and the above suggestions fix the issue 99% of the time

answered Aug 17, 2011 at 8:23

Julian Suarez's user avatar

Julian SuarezJulian Suarez

4,5074 gold badges24 silver badges40 bronze badges

To solve this issue, I pasted my layout into https://www.xmlvalidation.com/, which told me exactly what the error was. As was the case with other answers, my XML had < in a string.

answered Apr 20, 2018 at 18:00

Alan Kinnaman's user avatar

Remove the semicolon after hello

answered Nov 30, 2013 at 9:12

Vincent Thacker's user avatar

I had the same problem. In my case, even though I have not understood why, the problem was due to & in one of the elements like the following where a and b are two tokens/words:

<s> . . . a & b . . . </s>

and to resolve the issue I turned my element’s text to the following:

<s> . . . a and b . . . </s>

I thought it might be the case for some of you. Generally, to make your life easier, just go and read the character at the index mentioned in the error message (line:..., col:...) and see what the character is.

answered Nov 18, 2019 at 0:10

Pedram's user avatar

PedramPedram

2,3892 gold badges31 silver badges48 bronze badges

In my case I forgot to end my ConstrainLayout

</android.support.constraint.ConstraintLayout>

After that, everything started working correctly.

answered Dec 6, 2018 at 10:18

Wiktor Kalinowski's user avatar

0

I tried everything on my end and ended up with the following.

I had the first line as:

<?xmlversion="1.0"encoding="utf-8"?>

And I was missing two spaces there, and it should be:

<?xml version="1.0" encoding="utf-8"?>

Before the version and before the encoding there should be a space.

jkdev's user avatar

jkdev

11.3k15 gold badges54 silver badges77 bronze badges

answered Jul 2, 2019 at 13:30

Michael Fahim's user avatar

Problem is that you are doing something wrong in XML layout file

android:text=" <- Go Back" // this creates error
android:text="Go Back" // correct way

answered Nov 29, 2020 at 7:35

Vijay's user avatar

VijayVijay

1,1558 silver badges22 bronze badges

I’ve come across another APK that’s suffering from this issue:

Information

Apktool Version: 2.2.2
Operating System: Both Linux and Mac
APK From: https://forum.xda-developers.com/android/apps-games/ps4-remote-play-android-thread-t3068225

Steps to Reproduce

$ apktool d RemotePlayPortV5.1_ITB.apk
...
$ apktool b RemotePlayPortV5.1_ITB

I: Using Apktool 2.2.2 on RemotePlayPortV5.1_ITB.apk
I: Loading resource table...
I: Decoding AndroidManifest.xml with resources...
I: Loading resource table from file: /root/.local/share/apktool/framework/1.apk
I: Regular manifest package...
I: Decoding file-resources...
I: Decoding values */* XMLs...
I: Baksmaling classes.dex...
I: Copying assets and libs...
I: Copying unknown files...
I: Copying original files...
root@99033f046f3d:/usr/src/apk# apktool b RemotePlayPortV5.1_ITB
I: Using Apktool 2.2.2
I: Checking whether sources has changed...
I: Smaling smali folder into classes.dex...
I: Checking whether resources has changed...
I: Building resources...
W: /usr/src/apk/RemotePlayPortV5.1_ITB/res/layout/companionutil_layout_alert_dialog.xml:2: error: Error parsing XML: not well-formed (invalid token)
W:
W: /usr/src/apk/RemotePlayPortV5.1_ITB/res/layout/companionutil_layout_alert_dialog_game2_confirm.xml:2: error: Error parsing XML: not well-formed (invalid token)
W:
W: /usr/src/apk/RemotePlayPortV5.1_ITB/res/layout/companionutil_layout_alert_dialog_game_confirm.xml:2: error: Error parsing XML: not well-formed (invalid token)
W:
Exception in thread "main" brut.androlib.AndrolibException: brut.androlib.AndrolibException: brut.common.BrutException: could not exec (exit code = 1): [/tmp/brut_util_Jar_4284272564605293496.tmp, p, --forced-package-id, 127, --min-sdk-version, 17, --target-sdk-version, 19, --version-code, 10500, --version-name, 1.5.0, --no-version-vectors, -F, /tmp/APKTOOL6062192940819296925.tmp, -0, arsc, -0, arsc, -I, /root/.local/share/apktool/framework/1.apk, -S, /usr/src/apk/RemotePlayPortV5.1_ITB/res, -M, /usr/src/apk/RemotePlayPortV5.1_ITB/AndroidManifest.xml]
	at brut.androlib.Androlib.buildResourcesFull(Androlib.java:477)
	at brut.androlib.Androlib.buildResources(Androlib.java:411)
	at brut.androlib.Androlib.build(Androlib.java:310)
	at brut.androlib.Androlib.build(Androlib.java:263)
	at brut.apktool.Main.cmdBuild(Main.java:227)
	at brut.apktool.Main.main(Main.java:84)
Caused by: brut.androlib.AndrolibException: brut.common.BrutException: could not exec (exit code = 1): [/tmp/brut_util_Jar_4284272564605293496.tmp, p, --forced-package-id, 127, --min-sdk-version, 17, --target-sdk-version, 19, --version-code, 10500, --version-name, 1.5.0, --no-version-vectors, -F, /tmp/APKTOOL6062192940819296925.tmp, -0, arsc, -0, arsc, -I, /root/.local/share/apktool/framework/1.apk, -S, /usr/src/apk/RemotePlayPortV5.1_ITB/res, -M, /usr/src/apk/RemotePlayPortV5.1_ITB/AndroidManifest.xml]
	at brut.androlib.res.AndrolibResources.aaptPackage(AndrolibResources.java:440)
	at brut.androlib.Androlib.buildResourcesFull(Androlib.java:463)
	... 5 more
Caused by: brut.common.BrutException: could not exec (exit code = 1): [/tmp/brut_util_Jar_4284272564605293496.tmp, p, --forced-package-id, 127, --min-sdk-version, 17, --target-sdk-version, 19, --version-code, 10500, --version-name, 1.5.0, --no-version-vectors, -F, /tmp/APKTOOL6062192940819296925.tmp, -0, arsc, -0, arsc, -I, /root/.local/share/apktool/framework/1.apk, -S, /usr/src/apk/RemotePlayPortV5.1_ITB/res, -M, /usr/src/apk/RemotePlayPortV5.1_ITB/AndroidManifest.xml]
	at brut.util.OS.exec(OS.java:95)
	at brut.androlib.res.AndrolibResources.aaptPackage(AndrolibResources.java:434)
	... 6 more

Example invalid .xml:

<?xml version="1.0" encoding="utf-8"?>
<o.ﺗ android:layout_gravity="center" android:orientation="vertical" android:background="@drawable/companionutil_drawable_alert_dialog" android:layout_width="fill_parent" android:layout_height="wrap_content" landscape_marginLeft="33dp" landscape_marginRight="33dp" portrait_marginLeft="11dp" portrait_marginRight="11dp"
  xmlns:android="http://schemas.android.com/apk/res/android">
    <com.playstation.companionutil.CompanionUtilAdjustTextView android:textSize="16.0dip" android:textColor="#ffffffff" android:id="@id/com_playstation_companionutil_id_alert_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="11.0dip" android:layout_marginTop="17.0dip" android:layout_marginRight="11.0dip" android:text="" android:lineSpacingExtra="1.0dip" />
    <com.playstation.companionutil.CompanionUtilAdjustButton android:textSize="16.0dip" android:textColor="#ffffffff" android:id="@id/com_playstation_companionutil_id_alert_positive_button" android:background="@drawable/companionutil_drawable_alert_dialog_button" android:layout_width="fill_parent" android:layout_height="28.0dip" android:layout_marginLeft="11.0dip" android:layout_marginTop="15.0dip" android:layout_marginRight="11.0dip" android:layout_marginBottom="15.0dip" android:text="@string/com_playstation_companionutil_msg_ok" />
</o.ﺗ>

APK

PS4 Remote Play Port
(https://forum.xda-developers.com/android/apps-games/ps4-remote-play-android-thread-t3068225)

Questions to ask before submission

Have you tried apktool d, apktool b without changing anything? Yes
If you are trying to install a modified apk, did you resign it? No
Are you using the latest apktool version? Yes

Suppose I had the following tags in my XML file:

<?xml version="1.0" encoding="utf-8"?>
<jobs>
<job>
<P class="Beaton"><FONT size=3><SPAN style="FONT-FAMILY: Symbol; COLOR: black; mso-ascii-font-family: 'Times New Roman'">�</SPAN><SPAN style="COLOR: black"><FONT face="Times New Roman"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Position accountability<o:p></o:p></FONT></SPAN></FONT></P>
<P class="Beaton"><FONT size=3><SPAN style="FONT-FAMILY: Symbol; COLOR: black; mso-ascii-font-family: 'Times New Roman'">�</SPAN><SPAN style="COLOR: black"><FONT face="Times New Roman"> <SPAN style="mso-spacerun: yes">&nbsp;</SPAN>55 FTEs <o:p></o:p></FONT></SPAN></FONT></P>
</job>
</jobs>

and below is my code:

from xml.sax.handler import ContentHandler
import xml.sax

xml_path = 'windows/xml_file.xml'

try:
    parser = xml.sax.make_parser( )
    parser.parse(open(xml_path))

except (xml.sax.SAXParseException), e:
        print "*** PARSER error: %s" % e

Result :
*** PARSER error: windows/xml_file.xml:4:113: not well-formed <invalid token>

Can anyone tell me what’s wrong in the p tag and how to avoid this kind of error?

Santosh Kumar's user avatar

Santosh Kumar

26.2k19 gold badges65 silver badges118 bronze badges

asked Jul 23, 2012 at 13:31

Shiva Krishna Bavandla's user avatar

The problem is probably with your FONT tag; the value of the size attribute should be quoted, otherwise this is simply not valid XML.

You might also run into problems with &nbsp;, which is not a valid XML entity (although it is valid in XHTML). Also, your <jobs> tag is not closed properly; the last line should be </jobs>.

In general, if you have problems with reading XML files, the first thing is always to check whether the XML file is well-formatted. One possible way to do that is by entering it into the W3C validator.

answered Jul 23, 2012 at 13:34

Tamás's user avatar

11

Chrome7
замечал, что это не корректный способ, но в моем случае он отработал идеально.

Завтра тебе пришлют новый документ, в котором будет смесь из амперсандов и сущностей — и твой сегодняшний код сломает завтрашний документ, повредив сущности.

То есть ты написал костыль. Пока человек идёт на костылях по улице, это выглядит приемлемо и похоже на обычного человека без костылей, но если вдруг ему нужно будет успеть на автобус, двери которого вот-вот закроются, то он не только его не догонит, но ещё и упадёт прямо на улице, не удержав костыли при беге, и будет очень долго валяться. Что хаки, что костыли, — это всё вредные вещи, которые на первый взгляд выглядят прекрасно, но эта прекрасность обманчива и очень часто потом выходит таким боком, что даже изсправить ничего не возможно, остаётся только всю программу заново писать, либо отказаться от её применения к новым данным.

Chrome7
Подробнее о том, как это корректно сделать можно прочитать здесь:

Во-первых, у него слишком короткое регулярное выражение, так как сущностей в XML может быть больше.
https://www.w3.org/TR/xml-entity-names/

И символы юникода также могут кодироваться через числовой код (в 10-чной и/или 16-ричной системе) с помощью амперсанда.
https://en.wikipedia.org/wiki/XML

All permitted Unicode characters may be represented with a numeric character reference.

Во-вторых, даже если просто заменить кавычку с сущности &apos; на простую кавычку, его код не сможет это исправить, потому что расчитан только на неправильно выраженный амперсанд в xml-документе, и исправить свой код, чтобы он ещё и кавычку учитывал, он не сможет, так как у кавычки гораздо больше смежных случаев, чем у амперсанда.

А как же это исправить? Выше я написал, как: надо сделать простейший транслятор, который сначала идентифицирует именно тот амперсанд в контексте и/или именно ту кавычку в контексте и только после этого заменит их. При этом будет гарантировано, что он не заменяет не тот амперсанд или не ту кавычку. То есть нужно написать лексический анализатор на базе ДКА. В общем случае понадобится магазин, но для частного случая можно и без магазина обойтись. Вот из-за того, что этот парнишка не знает программирование глубже школьника, он и побирается по всяким сайтам и в конечном итоге приходит к полурабочему решению.

Отредактировано py.user.next (Апрель 8, 2019 23:29:25)

Solution 1

I think you forgot to define the encoding type.
I suggest that you try to initialize that xml file to a string variable:

import xml.etree.ElementTree as ET
import xmltodict
import json


tree = ET.parse('your_data.xml')
xml_data = tree.getroot()
#here you can change the encoding type to be able to set it to the one you need
xmlstr = ET.tostring(xml_data, encoding='utf-8', method='xml')

data_dict = dict(xmltodict.parse(xmlstr))

Solution 2

In my case the file was being saved with a Byte Order Mark as is the default with notepad++

I resaved the file without the BOM to plain utf8.

Solution 3

Python 3

One Liner

data: dict = xmltodict.parse(ElementTree.tostring(ElementTree.parse(path).getroot()))

Helper for .json and .xml

I wrote a small helper function to load .json and .xml files from a given path.
I thought it might come in handy for some people here:

import json
import xml.etree.ElementTree

def load_json(path: str) -> dict:  
    if path.endswith(".json"):
        print(f"> Loading JSON from '{path}'")
        with open(path, mode="r") as open_file:
            content = open_file.read()

        return json.loads(content)
    elif path.endswith(".xml"):
        print(f"> Loading XML as JSON from '{path}'")
        xml = ElementTree.tostring(ElementTree.parse(path).getroot())
        return xmltodict.parse(xml, attr_prefix="@", cdata_key="#text", dict_constructor=dict)

    print(f"> Loading failed for '{path}'")
    return {}

Notes

  • if you want to get rid of the @ and #text markers in the json output, use the parameters attr_prefix="" and cdata_key=""

  • normally xmltodict.parse() returns an OrderedDict but you can change that with the parameter dict_constructor=dict

Usage

path = "my_data.xml"
data = load_json(path)
print(json.dumps(data, indent=2))

# OUTPUT
#
# > Loading XML as JSON from 'my_data.xml' 
# {
#   "mydocument": {
#     "@has": "an attribute",
#     "and": {
#       "many": [
#         "elements",
#         "more elements"
#       ]
#     },
#     "plus": {
#       "@a": "complex",
#       "#text": "element as well"
#     }
#   }
# }

Sources

  • ElementTree.tostring()
  • ElementTree.parse()
  • xmltodict
  • json.dumps()

Related videos on Youtube

PyQClic PyqClic is a quiz based educational application

02 : 30

PyQClic PyqClic is a quiz based educational application

Comments

  • When I use xmltodict to load the xml file below I get an error:
    xml.parsers.expat.ExpatError: not well-formed (invalid token): line 1, column 1

    Here is my file:

    <?xml version="1.0" encoding="utf-8"?>
    <mydocument has="an attribute">
      <and>
        <many>elements</many>
        <many>more elements</many>
      </and>
      <plus a="complex">
        element as well
      </plus>
    </mydocument>
    

    Source:

    import xmltodict
    with open('fileTEST.xml') as fd:
       xmltodict.parse(fd.read())
    

    I am on Windows 10, using Python 3.6 and xmltodict 0.11.0

    If I use ElementTree it works

    tree = ET.ElementTree(file='fileTEST.xml')
        for elem in tree.iter():
                print(elem.tag, elem.attrib)
    
    mydocument {'has': 'an attribute'}
    and {}
    many {}
    many {}
    plus {'a': 'complex'}
    

    Note: I might have encountered a new line problem.
    Note2: I used Beyond Compare on two different files.
    It crashes on the file that is UTF-8 BOM encoded, and works om the UTF-8 file.
    UTF-8 BOM is a sequence of bytes (EF BB BF) that allows the reader to identify a file as being encoded in UTF-8.

  • Obviously it would work without the xml declaration; did it fail with it? For me, it didn’t fail.

  • For me it failed with xml declaration, but not without it.. You know, I try things before posting it here

  • I’m not trying to throw shade. I got a different result, and it wasn’t clear to me that you had tried with the declaration. What versions are you using? I’m using Python 3.5.2, Expat 2.1.1

  • Python: 3.6.3, xmltodict: 0.11.0.

  • oddly, this worked for me without changing from the default encoding type, which is set to 'utf-8' on both ET and xmltodict

Recents

Related

Понравилась статья? Поделить с друзьями:
  • Ошибка not supported between instances of int and str
  • Ошибка not support this platform
  • Ошибка none of the available
  • Ошибка not sdl file в тылу врага 2
  • Ошибка non system disk or disk error