Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need help for xml
#1
XML
can some one please guide me through this one . I am trying to make an xml from the data below .
Drug XML
Create an XML structure that describes a set of drugs and the possible dosages they might have based on the table below.
Create an element named <drug> for each drug entry.
Make the name, dosage, units, and cost attributes of each drug. Note that the cost is the cost per pill rather than the cost mg.
Make the count the text for that drug element.
Name Dosage Units Cost Count
Asprin 100 mg 0.10 320
Asprin 200 mg 0.15 211
Digoxin 10 mL 1.22 19
Digoxin 20 mL 2.01 27
Below is my code
`import xml.etree.ElementTree as xml
drugs = """<?xml version="1.0"?>
<drugs>
<drug1 Name="Aspirin" Dosage ="100" Units="mg" Cost= "0.10">
Count = 320
</start>
<drug1 Name="Aspirin" Dosage ="200" Units="mg" Cost= "0.15">
Count =211
</start>
<drug1 Name="Digoxin" Dosage ="10" Units="mL" Cost= "1.22">
Count =19

</start>
<drug1 Name="Digoxin" Dosage ="20" Units="mL" Cost= "2.01">
Count =27

</start>



</drugs>
"""
` I am not very sure is it correct or not .Please guide me .
Thanks
Reply
#2
Split from http://python-forum.io/Thread-json since XML is not JSON.

Do you have code you've tried?
Reply
#3
Hi , I am trying to write data in xml > please look at my code and guide me to correct it . 
`Create an XML structure that describes a set of drugs and the possible dosages they might have based on the table below.
Create an element named <drug> for each drug entry.
Make the name, dosage, units, and cost attributes of each drug. Note that the cost is the cost per pill rather than the cost mg.
Make the count the text for that drug element.
Name     Dosage    Units   Cost   Count
Asprin   100       mg      0.10   320
Asprin   200       mg      0.15   211
Digoxin  10        mL      1.22   19
Digoxin  20        mL      2.01   27
`
import xml.etree.ElementTree as xml
drugs = """<?xml version="1.0"?>
<drugs>
 <drug1 Name="Aspirin" Dosage ="100" Units="mg" Cost= "0.10">
 Count = 320
 </start>
 <drug1 Name="Aspirin" Dosage ="200" Units="mg" Cost= "0.15">
 Count =211
 </start>
 <drug1 Name="Digoxin" Dosage ="10" Units="mL" Cost= "1.22">
 Count =19
 
 </start>
 <drug1 Name="Digoxin" Dosage ="20" Units="mL" Cost= "2.01">
 Count =27
 
 </start>
 


</drugs>
"""
Reply
#4
<Merged threads>
Reply
#5
Hi , I am trying to write data in xml > please look at my code and guide me to correct it . 
`Create an XML structure that describes a set of drugs and the possible dosages they might have based on the table below.
Create an element named <drug> for each drug entry.
Make the name, dosage, units, and cost attributes of each drug. Note that the cost is the cost per pill rather than the cost mg.
Make the count the text for that drug element.
Name     Dosage    Units   Cost   Count
Asprin   100       mg      0.10   320
Asprin   200       mg      0.15   211
Digoxin  10        mL      1.22   19
Digoxin  20        mL      2.01   27
`
import xml.etree.ElementTree as xml
drugs = """<?xml version="1.0"?>
<drugs>
 <drug1 Name="Aspirin" Dosage ="100" Units="mg" Cost= "0.10">
 Count = 320
 </start>
 <drug1 Name="Aspirin" Dosage ="200" Units="mg" Cost= "0.15">
 Count =211
 </start>
 <drug1 Name="Digoxin" Dosage ="10" Units="mL" Cost= "1.22">
 Count =19
 
 </start>
 <drug1 Name="Digoxin" Dosage ="20" Units="mL" Cost= "2.01">
 Count =27
 
 </start>
 


</drugs>
"""

User has been warned for this post. Reason: Twice, created a new thread when a suitable one already existed, the second time after the first had been merged.
Reply
#6
I've merged another thread. Stop creating new ones. Stick to this thread. You also still need to write your own code, as we're not going to do it for you.
Reply
#7
yes i did write my code ON TOP
Reply
#8
He means you must of created 3 separate new threads, in which he merged together in this one thread.
Recommended Tutorials:
Reply
#9
If that's your code, then your question isn't really about Python. You imported a module and assigned a variable, two separate things which ultimately don't really do anything. And to create the structure is ambiguous instructions. Just assigning a string to a variable probably isn't what they want, but what they actually want isn't clear.
Reply
#10
The first step, would be to create valid xml. You probably don't want the content of each node to be "Count = {number}", either that should be another attribute, or the content should just be whatever number it is. Also, if your tag name is "drug1", your closing tag should also be "drug1". Having different opening and closing tags is...not valid xml.

So, instead of:
 <drug1 Name="Digoxin" Dosage ="20" Units="mL" Cost= "2.01">
     Count =27
 </start>
Try:
 <drug1 Name="Digoxin" Dosage ="20" Units="mL" Cost= "2.01" Count="27" />
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020