How to add special characters to xml tag using lxml?
I am trying to create xml file using lxml as below
from lxml import etree
var = "xyz"
root = etree.Element("Demo_root")
a = etree.SubElement(root, "Demo2='"+var+"'")
getting error as
invalid tag name
at line number 4 in the code, I can't able to give any special characters. So how can i handle it.
My file should be look as
<x Name="dem1">
<y Name="dem2" Value="1"/>
<y Name="dem3" Value="2"/>
</x>
python-2.7 lxml
add a comment |
I am trying to create xml file using lxml as below
from lxml import etree
var = "xyz"
root = etree.Element("Demo_root")
a = etree.SubElement(root, "Demo2='"+var+"'")
getting error as
invalid tag name
at line number 4 in the code, I can't able to give any special characters. So how can i handle it.
My file should be look as
<x Name="dem1">
<y Name="dem2" Value="1"/>
<y Name="dem3" Value="2"/>
</x>
python-2.7 lxml
Is there anyway to do it?
– praveen jp
Nov 16 '18 at 11:19
Any way to do what? Please read xml.com/pub/a/2001/07/25/namingparts.html
– mzjn
Nov 16 '18 at 11:20
1
Are you trying to add an attribute instead of creating an element? Please add an example of what the XML should look like.
– Daniel Haley
Nov 16 '18 at 16:43
@DanielHaley i have edited my question, now you may get clear idea about how it looks.
– praveen jp
Nov 19 '18 at 6:00
add a comment |
I am trying to create xml file using lxml as below
from lxml import etree
var = "xyz"
root = etree.Element("Demo_root")
a = etree.SubElement(root, "Demo2='"+var+"'")
getting error as
invalid tag name
at line number 4 in the code, I can't able to give any special characters. So how can i handle it.
My file should be look as
<x Name="dem1">
<y Name="dem2" Value="1"/>
<y Name="dem3" Value="2"/>
</x>
python-2.7 lxml
I am trying to create xml file using lxml as below
from lxml import etree
var = "xyz"
root = etree.Element("Demo_root")
a = etree.SubElement(root, "Demo2='"+var+"'")
getting error as
invalid tag name
at line number 4 in the code, I can't able to give any special characters. So how can i handle it.
My file should be look as
<x Name="dem1">
<y Name="dem2" Value="1"/>
<y Name="dem3" Value="2"/>
</x>
python-2.7 lxml
python-2.7 lxml
edited Nov 19 '18 at 5:59
praveen jp
asked Nov 16 '18 at 10:48
praveen jppraveen jp
288
288
Is there anyway to do it?
– praveen jp
Nov 16 '18 at 11:19
Any way to do what? Please read xml.com/pub/a/2001/07/25/namingparts.html
– mzjn
Nov 16 '18 at 11:20
1
Are you trying to add an attribute instead of creating an element? Please add an example of what the XML should look like.
– Daniel Haley
Nov 16 '18 at 16:43
@DanielHaley i have edited my question, now you may get clear idea about how it looks.
– praveen jp
Nov 19 '18 at 6:00
add a comment |
Is there anyway to do it?
– praveen jp
Nov 16 '18 at 11:19
Any way to do what? Please read xml.com/pub/a/2001/07/25/namingparts.html
– mzjn
Nov 16 '18 at 11:20
1
Are you trying to add an attribute instead of creating an element? Please add an example of what the XML should look like.
– Daniel Haley
Nov 16 '18 at 16:43
@DanielHaley i have edited my question, now you may get clear idea about how it looks.
– praveen jp
Nov 19 '18 at 6:00
Is there anyway to do it?
– praveen jp
Nov 16 '18 at 11:19
Is there anyway to do it?
– praveen jp
Nov 16 '18 at 11:19
Any way to do what? Please read xml.com/pub/a/2001/07/25/namingparts.html
– mzjn
Nov 16 '18 at 11:20
Any way to do what? Please read xml.com/pub/a/2001/07/25/namingparts.html
– mzjn
Nov 16 '18 at 11:20
1
1
Are you trying to add an attribute instead of creating an element? Please add an example of what the XML should look like.
– Daniel Haley
Nov 16 '18 at 16:43
Are you trying to add an attribute instead of creating an element? Please add an example of what the XML should look like.
– Daniel Haley
Nov 16 '18 at 16:43
@DanielHaley i have edited my question, now you may get clear idea about how it looks.
– praveen jp
Nov 19 '18 at 6:00
@DanielHaley i have edited my question, now you may get clear idea about how it looks.
– praveen jp
Nov 19 '18 at 6:00
add a comment |
1 Answer
1
active
oldest
votes
It looks like what you're trying to do is create an attribute and not a child element. (It's still hard to tell since the element/attribute names in your XML don't match what's in your code.)
To create an attribute, you can use set()
.
Example to create the XML in your example...
from lxml import etree
var = "dem1" # A variable isn't needed, but I included it anyway since your original code had it.
root = etree.Element("x")
root.set("Name", var)
for x in range(1, 3):
elem = etree.Element("y")
elem.set("Name", "dem{}".format(x + 1))
elem.set("Value", str(x))
root.append(elem)
etree.dump(root)
Output...
<x Name="dem1">
<y Name="dem2" Value="1"/>
<y Name="dem3" Value="2"/>
</x>
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336316%2fhow-to-add-special-characters-to-xml-tag-using-lxml%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It looks like what you're trying to do is create an attribute and not a child element. (It's still hard to tell since the element/attribute names in your XML don't match what's in your code.)
To create an attribute, you can use set()
.
Example to create the XML in your example...
from lxml import etree
var = "dem1" # A variable isn't needed, but I included it anyway since your original code had it.
root = etree.Element("x")
root.set("Name", var)
for x in range(1, 3):
elem = etree.Element("y")
elem.set("Name", "dem{}".format(x + 1))
elem.set("Value", str(x))
root.append(elem)
etree.dump(root)
Output...
<x Name="dem1">
<y Name="dem2" Value="1"/>
<y Name="dem3" Value="2"/>
</x>
add a comment |
It looks like what you're trying to do is create an attribute and not a child element. (It's still hard to tell since the element/attribute names in your XML don't match what's in your code.)
To create an attribute, you can use set()
.
Example to create the XML in your example...
from lxml import etree
var = "dem1" # A variable isn't needed, but I included it anyway since your original code had it.
root = etree.Element("x")
root.set("Name", var)
for x in range(1, 3):
elem = etree.Element("y")
elem.set("Name", "dem{}".format(x + 1))
elem.set("Value", str(x))
root.append(elem)
etree.dump(root)
Output...
<x Name="dem1">
<y Name="dem2" Value="1"/>
<y Name="dem3" Value="2"/>
</x>
add a comment |
It looks like what you're trying to do is create an attribute and not a child element. (It's still hard to tell since the element/attribute names in your XML don't match what's in your code.)
To create an attribute, you can use set()
.
Example to create the XML in your example...
from lxml import etree
var = "dem1" # A variable isn't needed, but I included it anyway since your original code had it.
root = etree.Element("x")
root.set("Name", var)
for x in range(1, 3):
elem = etree.Element("y")
elem.set("Name", "dem{}".format(x + 1))
elem.set("Value", str(x))
root.append(elem)
etree.dump(root)
Output...
<x Name="dem1">
<y Name="dem2" Value="1"/>
<y Name="dem3" Value="2"/>
</x>
It looks like what you're trying to do is create an attribute and not a child element. (It's still hard to tell since the element/attribute names in your XML don't match what's in your code.)
To create an attribute, you can use set()
.
Example to create the XML in your example...
from lxml import etree
var = "dem1" # A variable isn't needed, but I included it anyway since your original code had it.
root = etree.Element("x")
root.set("Name", var)
for x in range(1, 3):
elem = etree.Element("y")
elem.set("Name", "dem{}".format(x + 1))
elem.set("Value", str(x))
root.append(elem)
etree.dump(root)
Output...
<x Name="dem1">
<y Name="dem2" Value="1"/>
<y Name="dem3" Value="2"/>
</x>
answered Nov 19 '18 at 19:20
Daniel HaleyDaniel Haley
39.6k45481
39.6k45481
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336316%2fhow-to-add-special-characters-to-xml-tag-using-lxml%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is there anyway to do it?
– praveen jp
Nov 16 '18 at 11:19
Any way to do what? Please read xml.com/pub/a/2001/07/25/namingparts.html
– mzjn
Nov 16 '18 at 11:20
1
Are you trying to add an attribute instead of creating an element? Please add an example of what the XML should look like.
– Daniel Haley
Nov 16 '18 at 16:43
@DanielHaley i have edited my question, now you may get clear idea about how it looks.
– praveen jp
Nov 19 '18 at 6:00