Groovy Script to remove the xml declaration and parent nodes and display only childnodes - groovy

I am trying to transform xml using groovy script but failing, Any help will be highly appreciated
My input is xml is below
<?xml version="1.0" encoding="UTF-8"?>
<rsp stat="ok" version="1.0">
<result>
<total_results>700</total_results>
<emailClick>
<id>993</id>
<created_at>2023-02-14 00:00:10</created_at>
</emailClick>
<emailClick>
<id>995</id>
<created_at>2023-02-14 00:00:10</created_at>
</emailClick>
</result>
</rsp>
My output will display only childnodes without xml declaration and parent xml tags
<emailClick>
<id>993</id>
<created_at>2023-02-14 00:00:10</created_at>
</emailClick>
<emailClick>
<id>995</id>
<created_at>2023-02-14 00:00:10</created_at>
</emailClick>
I tried getting the nodelist using xmlparser and then convert that into string but it is resulting in error

Related

How to get the xml element path by using xml.etree.elementtree instead of lxml?

I'm new to python, I'm using xml.etree.elementtree module to parse the xml file. while parsing trying to get the xml element path which is having the value.
for example:
<root>
<a>
<b>Hello</b>
<b1>Hi</b1>
</a>
<c>
<d>Welcome</d>
<e>Sample xml</e>
</c>
</root>
here I want to get the element path, which is having value
root/a/b
root/a/b1
root/c/d
root/c/e
like in lxml module we can get it with the help of getelement(). But, how can we achieve the same by using the module xml.etree.elementtree.

How to parse big XML file using beautiful soup?

I am trying to parse an XML file named document.xml which contains around 400000 character (including tags, breakline, space) init find the code below
document_xml_file_object = open('document.xml', 'r')
document_xml_file_content = document_xml_file_object.read()
xml_content = BeautifulSoup(document_xml_file_content, 'lxml-xml')
print("XML CONTENT: ", xml_content)
when I am printing xml_content below is my output:
XML CONTENT: <?xml version="1.0" encoding="utf-8"?>
For the smaller size of files its printing complete XML code. can anyone help me with this why its happening.
Edit : Click Here to see my XML Content.
Thanks in Advance
For large files it better to use line parser like xml.sax. beautifulsoup will load the whole file in memory and parse, while using xml.sax you will use quite less memory.

Multiple attributes for one SubElement via Python

I'm writing a SOAP xml file in Python using etree
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns2="urn:hss">
<SOAP-ENV:Body>
<ns2:add>
<subscriberList>
<subscriber name="Sub_Test_Profile_0000000002">
<uiccList>
<uicc imsi="313:78:0000000002">
<profile profile="100Mbps_Data"/>
<k>7169B9E06940966BF7C4DD3EDD12B06F</k>
<opc>29EFECFF965CE59587C054FA319D43DC</opc>
</uicc>
</uiccList>
</subscriber>
</subscriberList>
</ns2:add>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
**I'm trying to figure out how to represent the uicc imsi like the example below with multiple attributes.
<uiccList>
<uicc imsi="313:78:0000000003" profile="100Mbps_Data">
</uicc>
</uiccList>
I was making it more difficult than it needed to be. All that was required was a comma between attributes.
uicc = etree.SubElement(uiccList, "uicc" ,{'imsi':'313:78:' +val, 'profile':"100Mbps_Data"})

How to add xmi:version="2.0" attribute to an element

I am creating a xml file. i am done with the root element creation and i am able to define xml declaration. But i need to create anther tag, which looks like
<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:TalendProperties="http://www.talend.org/properties">
# i am unable to replicate the above
### some subelements..
</xmi:XMI>
i am done with adding xmlns URIs, but unable to get the xmi:version="2.0".
I am not familiar with XML, so getting confused, read about namespace and all, not quite getting it. Can somebody show me how to do that or share a related weblink. That woul dbe great help. Because i found mostly the XML parsing stuff on internet but very few resource on XML generaton.
xmlns_uris_dict = {'xmi':'http://..', 'subprocess':'http://xyz...'}
root = ET.Element("talendfile:ProcessType")
ET.register_namespace('xmi', 'version="2.0"') # This part gives a wrong presentation.
# i am able to add URIs here
for prefix, uri in xmlns_uris_dict.items():
root.attrib['xmlns:' + prefix] = uri
A good way to create namespaced elements and attributes is to use QName.
import xml.etree.ElementTree as ET
NS = "http://www.omg.org/XMI"
ET.register_namespace("xmi", NS)
# Create xmi:XMI element
root = ET.Element(ET.QName(NS, "XMI"))
# Add xmi:version attribute
root.set(ET.QName(NS, "version"), "2.0")
print(ET.tostring(root).decode())
Result:
<xmi:XMI xmlns:xmi="http://www.omg.org/XMI" xmi:version="2.0" />
register_namespace() ensures that the xmi prefix (not the default ns0) is used when serializing the XML document.

Loop and merge XML files in a folder to one HTML file using Groovy script?

I am using ReadyAPI and trying to fetch my reports generation, so I'm at the point where all the xml files for each test case are generated, and I need to merge them.
So basically I only have a path where the files are, let's say "C:\Path", where XML files lie.
I have found parsers for single files, and ways to append some information of one XML file into another XML file, but I have not found the way to loop through all XML files and dump their content into a new file...
Any help or indication could be much appreciated...
Jackson.
There is a working example of this answer here.
Let's assume that we have XML files of this form:
<composer>
<name>Wolfgang Mozart</name>
<born>1756</born>
</composer>
Then, we could build a list of parsed XML documents from each .xml file in the current directory (or whichever you need):
def composers = []
new File(".").eachFile { def file ->
if (file.name ==~ /.*\.xml/) {
composers << new XmlSlurper().parse(file)
}
}
Then, we could use a StreamingMarkupBuilder to create a unified XML document. Note this mixes markup with the composers list built above:
def xml = new StreamingMarkupBuilder().bind {
root {
composers.each { c ->
mkp.yield c
}
}
}.toString()
That is, the document looks like:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<composer>
<name>Wolfgang Mozart</name>
<born>1756</born>
</composer>
<composer>
<name>JS Bach</name>
<born>1685</born>
</composer>
...
</root>
Altering the solution for your local goals should be straight-forward.

Resources