I'm working on my python script as I would like to change the language using xml when pressing on the enter button of the keyboard.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<strings>
<string id="32000">Hello UK</string>
</strings>
<control type="label">
<description>My hello label</description>
<posx>20</posx>
<posy>20</posy>
<width>180</width>
<height>248</height>
<align>middle</align>
<font>font12</font>
<textcolor>white</textcolor>
<visible>true</visible>
<label>$LOCALIZE[SCRIPT32000]</label>
</control>
Here is the python:
import xbmc
import xbmcgui
import xbmcaddon
#get actioncodes from keyboard.xml
ACTION_ENTER = 7
class MyClass(xbmcgui.WindowXML):
def onAction(self, action):
if action == ACTION_ENTER:
if image1_enabled:
my_hello_string = ADDON.getLocalizedString(32000)
I have got a problem with my python script, because when I press on the enter button, there are no text display on the screen. There are no error on the xbmc logs. I want to add the label to get the strings that I stored in xml to display the strings on the skins. Not sure if I have missing something?
If that is your complete code, it looks like you're not doing anything with the class. You might need to add to the end of your Python code something like:
if __name__ == '__main__':
w = MyClass("myclass.xml")
w.doModal()
Related
I am writing program to work on xml file and change it. But when I try to get to any part of it I get some extra part.
My xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>sbaa__ApprovalChain__c.ExternalID__c</members>
<members>sbaa__ApprovalCondition__c.ExternalID__c</members>
<members>sbaa__ApprovalRule__c.ExternalID__c</members>
<name>CustomField</name>
</types>
<version>40.0</version>
</Package>
And I have my code:
from lxml import etree
import sys
tree = etree.parse('package.xml')
root = tree.getroot()
print( root[0][0].tag )
As output I expect to see members but I get something like this:
{http://soap.sforce.com/2006/04/metadata}members
Why do I see that url and how to stop it from showing up?
You have defined a default namespace (Wikipedia, lxml tutorial). When defined, it is a part of every child tag.
If you want to print the tag without the namespace, it's easy
tag = root[0][0].tag
print(tag[tag.find('}')+1:])
If you want to remove the namespace from XML, see this question.
Consider the following XML code.
<?xml version="1.0"?>
<data>
<element>This is the first sentence.<button>Click</button>some more text.
</element>
</data>
I am using Python module xml.etree.ElementTree.
I know that I can access to elements and texts with the following Python code
import xml.etree.ElementTree as ET
name = 'data.xml'
tree = ET.parse(name)
root = tree.getroot()
element = root[0].tag
first_text = root[0].text #This is the first sentence
button = root[0][0].tag #button
buttontext = root[0][0].text #click
But how do I access to text "some more text" with Python?
I haven't found the solution yet...
You can also suggest some other Python module if it's better way to do that.
That XML-code is just an example.
you are looking for the .tail attribute of the <button> element: https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.tail
I want to create an application on OS X with Python, that is AppleScript-able.
First I used this tutorial to create an Application (it works!). Then I used this SO answer to add AppleScript support; I tried to translate the Objective-C stuff into Python. I added a plist-item to the options in setup.py:
OPTIONS = {
#...
'plist': {
'NSAppleScriptEnabled': True,
'OSAScriptingDefinition': 'SimpleXibDemo.sdef',
#...
}
}
I created SimpleXibDemo.sdef
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
<dictionary title="SimpleXibDemo">
<suite name="SimpleXibDemo Suite" code="SXiD" description="SimpleXibDemo Scripts">
<command name="increase" code="increxib" description="Increase the value">
<cocoa class="SimpleXibDemoIncreaseCommand"/>
<parameter name="by" type="integer" optional="yes">
<cocoa key="by"/>
</parameter>
<result description="Returns the new value" type="integer"/>
</command>
</suite>
</dictionary>
and I added the class SimpleXibDemoIncreaseCommand:
class SimpleXibDemoIncreaseCommand(NSScriptCommand):
def performDefaultImplementation(self):
args = self.evaluatedArguments()
nr = args.valueForKey("by")
viewController.increment()
return nr + 1
The application itself works, but when I run this AppleScript:
tell application "SimpleXibDemo"
set myResult to increase by 3
end tell
I get this error:
error "SimpleXibDemo got an error: Can’t continue increase." number -1708
I can't find any info on error number -1708. And when I open the application with Script Editor -> Open Dictionary, nothing happens.
What is the problem here? I'm a bit stuck :-/
I am taking an XML file as an input, have to search with a keyword i.e GENTEST05.
If found , then I need to pick up its parent node (in this example I want to pick up <ScriptElement>) and then replace the complete node <ScriptElement>blahblah</ScriptElement> with a new content.
...
...
<ScriptElement>
<ScriptElement>
<ScriptElement>
<ElementData xsi:type="anyData">
<DeltaTime>
<Area>
<Datatype>USER PROMPT [GENTEST05]</Datatype>
<Description />
<Multipartmessage>False<Multipartmessage>
<Comment>false</Comment>
</ElementData>
</ScriptElement>
<ScriptElement>
<ScriptElement>
...
...
...
I am trying to do this using Beautifulsoup. This is what I've done so far but not getting a proper way to proceed. Other than beautifulsoup, ElementTree or any other suggestion is welcome.
import sys
from BeautifulSoup import BeautifulStoneSoup as bs
xmlsoup = bs(open('file_xml' , 'r'))
a = raw_input('Enter Text')
paraText = xmlsoup.findAll(text=a)
print paraText
print paraText.findParent()
Ok here is some sample code to get you started. I used ElementTree because it's a builtin module and quite suitable for this type of task.
Here is the XML file I used:
<?xml version="1.0" ?>
<Script>
<ScriptElement/>
<ScriptElement/>
<ScriptElement>
<ElementData>
<DeltaTime/>
<Area/>
<Datatype>USER PROMPT [GENTEST05]</Datatype>
<Description/>
<Multipartmessage>False</Multipartmessage>
<Comment>false</Comment>
</ElementData>
</ScriptElement>
<ScriptElement/>
<ScriptElement/>
</Script>
Here is the python program:
import sys
import xml.etree.ElementTree as ElementTree
tree = ElementTree.parse("test.xml")
root = tree.getroot()
#The keyword to find and remove
keyword = "GENTEST05"
for element in list(root):
for sub in element.iter():
if sub.text and keyword in sub.text:
root.remove(element)
print ElementTree.tostring(root)
sys.exit()
I have kept the program simple so that you can improve on it. Since your XML has one root node, I am assuming you want to remove all parent elements of the keyword-matched element directly up to the root. In ElementTree, you can call root.remove() to remove the <ScriptElement> element that is the ancestory of the keyword-matched element.
This is just to get you started: this will only remove the first element, then print the resulting tree and quit.
Output:
<Script>
<ScriptElement />
<ScriptElement />
<ScriptElement />
<ScriptElement />
</Script>
I am trying to write a python program that uses DOM to read xml file and print another xml structure that list from only one node with particular selected attribute "fun".
<?xml version="1.0" encoding="ISO-8859-1"?>
<website>
<url category="fun">
<title>Fun world</title>
<author>Jack</author>
<year>2010</year>
<price>100.00</price>
</url>
<url category="entertainment">
<title>Fun world</title>
<author>Jack</author>
<year>2010</year>
<price>100.00</price>
</url>
</website>
I couldn't select the list from the URL having category="fun".
I tried this code:
for n in dom.getElementsByTagName('url'):
s = n.attribute['category']
if (s.value == "fun"):
print n.toxml()
Can you guys help to me to debug my code?
nb: One of your tags opens "Website" and attempts to close "website" - so you'll want to fix that one...
You've mentioned lxml.
from lxml import etree as et
root = et.fromstring(xml)
fun = root.xpath('/Website/url[#category="fun"]')
for node in fun:
print et.tostring(node)
Use getAttribute:
for n in dom.getElementsByTagName('url'):
if (n.getAttribute('category') == "fun"):
print(n.toxml())