I need to generate a standard XML as follows using python:
<?xml version="1.0" encoding="UTF-8"?>
<req:ShipmentValidateRequestAP xmlns:req="http://www.example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/schema">
<root>
<doc>
<field1>some value1</field1>
<field2>some vlaue2</field2>
</doc>
</root>
</req:ShipmentValidateRequestAP>
And this needed to be requested to a server URL:
https://sampletest-ea.example.com/XMLShippingServlet
Can anybody please help me to implement this ?
you can finish your work by the help of http://docs.python.org/2/library/xml.dom.minidom.html.
It is a simple libary
Related
I created an e-commerce site with django. I would like to integrate a cxml punchout to link the ecommerce site and the systems of my buyers,
I would like to know here how to configure the cXML file below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.2.028/cXML.dtd">
<cXML payloadID="?" timestamp="?" xml:lang="en-US">
<Header>
<From>
<Credential domain="?">
<Identity>?</Identity>
</Credential>
</From>
<To>
<Credential domain="?">
<Identity>?</Identity>
</Credential>
</To>
<Sender>
<Credential domain="?">
<Identity>?</Identity>
<CredentialMac type="FromSenderCredentials" algorithm="HMAC-SHA1-96" creationDate="?"
expirationDate="?">?</CredentialMac>
</Credential>
<UserAgent>Test</UserAgent>
</Sender>
</Header>
<Request deploymentMode="test">
<PunchOutSetupRequest operation="create">
<BuyerCookie>1234ABCD</BuyerCookie>
<Extrinsic name="User">which user</Extrinsic>
<BrowserFormPost><URL>https://example.com/?BrowserFormPost</URL></BrowserFormPost>
<Contact>
<Name xml:lang="en-US">myname</Name>
<Email>whichmail#email</Email>
</Contact>
<SupplierSetup><URL>https://example.com/?SupplierSetup</URL></SupplierSetup>
</PunchOutSetupRequest>
For other information how to inform them ???
to adapt it to my project
You can find the cXML user guide in the link below, if you want to set up your own service.
https://xml.cxml.org/current/cXMLUsersGuide.pdf
Alternatively you can employ an existing punchout service like Tradecentric (~$300 a month), Greenwing (~$160 a month) or InstaPunchout (~$30 a month).
Good luck!
<?xml version="1.0" encoding="utf-8"?>
<AcResponse
Command="hist"
TaskId="408709">
<element
name="/build.gradle"
id="93527">
<transaction
id="1117194"
type="promote"
time="1529083792"
user="soarfa99">
<comment>Automated promotion to parent stream by module build: jenkins-SC-MODULE-CS-SC-TRUNK-MedRec-DEV-CI-430</comment>
<version
virtual="11007/75"
real="36877/2"
virtualNamedVersion="CS-SC-TRUNK-INTG/75"
realNamedVersion="CS-SC-TRUNK-MedRec-DEV2_ar037601/2"
elem_type="text"
dir="no">
<issueNum>72768</issueNum>
</version>
</transaction>
<transaction
id="1111652"
type="promote"
time="1528100495"
user="dm041068">
<comment>SEDA file add- Debajyoti</comment>
<version
virtual="11007/74"
real="39225/1"
virtualNamedVersion="CS-SC-TRUNK-INTG/74"
realNamedVersion="CS-SC-TRUNK-CM-DEV-Debajyoti_dm041068/1"
elem_type="text"
dir="no">
<issueNum>72629</issueNum>
</version>
</transaction>
</element>
<streams>
<stream
id="11007"
name="CS-SC-TRUNK-INTG"
type="normal"/>
</streams>
</AcResponse>
This is the xml i am trying to parse, and i am trying to extract the attribute 'issueNum' with the following code:
tree=ET.parse(xml)
root=tree.getroot()
for item in root.findall('version'):
for child in item:
print(child.attrib['issueNum'])
Can you guys please help, get me the value of "issueNum".
You can use an xpath expression to find the values of issueNum:
from lxml import etree
xml = '''<?xml version="1.0" encoding="utf-8"?>
<AcResponse
Command="hist"
TaskId="408709">....'''
tree = etree.fromstring(xml)
issues = tree.xpath('//version/issueNum')
for issue in issues:
print(issue.text)
This prints:
72768
72629
I am using python 2.7 with lxml:
parser = etree.XMLParser(load_dtd=True, no_network=False, resolve_entities=True)
xml = etree.parse(data, parser=parser)
The resulting xml will parse data with external entities. It will grab files if the external entity is:
file:///home/text.txt
However, if we want the external entity to be processed to list the files in a directory:
file:///home/
the external entity variable is blank.
This is a debian machine.
Example that gets a file:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlset [
<!ENTITY list SYSTEM "file:///home/test.txt" >]>
<test>
<game>
<files>&list;</files>
</game>
</test>
Example that DOES NOT get a list of file names:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlset [
<!ENTITY list SYSTEM "file:///home/" >]>
<test>
<game>
<files>&list;</files>
</game>
</test>
I have a script that parses XML using lxml.etree:
from lxml import etree
parser = etree.XMLParser(load_dtd=True, resolve_entities=True)
tree = etree.parse('main.xml', parser=parser)
I need load_dtd=True and resolve_entities=True be have &emptyEntry; from globals.xml resolved:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE map SYSTEM "globals.xml" [
<!ENTITY dirData "${DATADIR}">
]>
<map
xmlns:map="http://my.dummy.org/map"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsschemaLocation="http://my.dummy.org/map main.xsd"
>
&emptyEntry; <!-- from globals.xml -->
<entry><key>KEY</key><value>VALUE</value></entry>
<entry><key>KEY</key><value>VALUE</value></entry>
</map>
with globals.xml
<?xml version="1.0" encoding="UTF-8"?>
<!ENTITY emptyEntry "<entry></entry>">
Now I would like to move from non-standard lxml to standard xml.etree. But this fails with my file because the load_dtd=True and resolve_entities=True is not supported by xml.etree.
Is there an xml.etree-way to have these entities resolved?
My trick is to use the external program xmllint
proc = subprocess.Popen(['xmllint','--noent',fname],stdout=subprocess.PIPE)
output = proc.communicate()[0]
tree = ElementTree.parse(StringIO.StringIO(output))
lxml is a right tool for the job.
But, if you want to use stdlib, then be prepared for difficulties and take a look at XMLParser's UseForeignDTD method. Here's a good (but hacky) example: Python ElementTree support for parsing unknown XML entities?
I'm writing an application configuration module that uses XML in its files. Consider the following example:
<?xml version="1.0" encoding="UTF-8"?>
<Settings>
<PathA>/Some/path/to/directory</PathA>
<PathB>/Another/path</PathB>
</Settings>
Now, I'd like to override certain elements in a different file that gets loaded afterwards. Example of the override file:
<?xml version="1.0" encoding="UTF-8"?>
<Settings>
<PathB>/Change/this/path</PathB>
</Settings>
When querying the document (with overrides) with XPath, I'd like to get this as the element tree:
<?xml version="1.0" encoding="UTF-8"?>
<Settings>
<PathA>/Some/path/to/directory</PathA>
<PathB>/Change/this/path</PathB>
</Settings>
This is similar to what Python's ConfigParser does with its read() method, but done with XML. How can I implement this?
You could convert the XML into an instance of Python class:
import lxml.etree as ET
import io
class Settings(object):
def __init__(self,text):
root=ET.parse(io.BytesIO(text)).getroot()
self.settings=dict((elt.tag,elt.text) for elt in root.xpath('/Settings/*'))
def update(self,other):
self.settings.update(other.settings)
text='''\
<?xml version="1.0" encoding="UTF-8"?>
<Settings>
<PathA>/Some/path/to/directory</PathA>
<PathB>/Another/path</PathB>
</Settings>'''
text2='''\
<?xml version="1.0" encoding="UTF-8"?>
<Settings>
<PathB>/Change/this/path</PathB>
</Settings>'''
s=Settings(text)
s2=Settings(text2)
s.update(s2)
print(s.settings)
yields
{'PathB': '/Change/this/path', 'PathA': '/Some/path/to/directory'}
Must you use XML? The same could be achieved with JSON much simpler:
Suppose this is the text from the first config file:
text='''
{
"PathB": "/Another/path",
"PathA": "/Some/path/to/directory"
}
'''
and this is the text from the second:
text2='''{
"PathB": "/Change/this/path"
}'''
Then to merge the to, you simply load each into a dict, and call update:
import json
config=json.loads(text)
config2=json.loads(text2)
config.update(config2)
print(config)
yields the Python dict:
{u'PathB': u'/Change/this/path', u'PathA': u'/Some/path/to/directory'}