Is there a way to include greater than or less than signs in an XML file?

前端 未结 5 549
别跟我提以往
别跟我提以往 2021-01-01 11:52

I have an XML file from a client that has greater than > and less than < signs in it and it fails an XML format check. Is there a way to get

相关标签:
5条回答
  • 2021-01-01 12:28

    The direct answer to your question:

    Is there a way to get around this without asking the client to fix the file ?

    is "no". The data you are getting is not valid XML, and you are correct in rejecting it. I highly recommend going back to the client and saying that they must provide valid XML, using Character Entity References as mentioned by David and Rahul.

    0 讨论(0)
  • 2021-01-01 12:28

    To answer your question plainly no, you cannot have an XML file with < or > in any of its value fields because the XML format uses these characters to signify the parent and child elements, e.g. <note>, <to>, <from>, etc.

    Expanding on my answer: When a Python script writes < or > using the XML library, the library translates them to &lt or &gt, respectively. I don't believe this is possible with that library since it is actually filtering out the < and > characters as well as the Character Entity References. This makes sense - the XML library is preventing you from disrupting the syntax used for the parent xml.etree.cElementTree.Element or any child xml.etree.cElementTree.SubElement object fields. For example, use the code block in this great answer to experiment:

    import xml.etree.cElementTree as ET
    
    root = ET.Element("root")
    doc = ET.SubElement(root, "doc")
    
    ET.SubElement(doc, "field1", name="blah").text = "some <value>"
    ET.SubElement(doc, "field2", name="asdfasd").text = "some <other value>"
    
    tree = ET.ElementTree(root)
    tree.write("filename.xml")
    

    This yields <root><doc><field1 name="blah">some &lt;value&gt;</field1><field2 name="asdfasd">some &lt;other value&gt;</field2></doc></root>.

    Prettifying it:

    <root>
        <doc>
            <field1 name="blah">
                some &lt;value&gt;
            </field1>
            <field2 name="asdfasd">
                some &lt;other value&gt;
            </field2>
        </doc>
    </root>
    

    However, there's nothing stopping you from adding these characters manually: read in the XML file and re-write it, adding text, even if it contains < or >. If you want a proper XML file though, just be sure that these characters are only used within comment fields.

    For your particular problem, you could read in the lines from the client's XML files, then either remove the < and > characters or, if the client requires them, move them to a commented portion of the line. Part of the challenge is that you have to leave in the <note>,`, etc. portions of the file... This is challenging but it would be possible!

    The following is what I'd expect the result to look like.

    <?xml version="1.0" encoding="UTF-8"?>
    
    <note Name="PrintPgmInfo VDD"> <!-- PrintPgmInfo <> VDD -->
     <to>Tove</to>
     <from>Jani</from>
     <heading>Reminder</heading>
     <body>Don't forget me this weekend!</body>
    </note>
    
    0 讨论(0)
  • 2021-01-01 12:30

    You will have to use XML escape characters:

    " to  &quot;
    ' to  &apos;
    < to  &lt;
    > to  &gt;
    & to  &amp;
    

    Google escaping characters in XML for more information.

    0 讨论(0)
  • 2021-01-01 12:36

    You can use the similarly looking full-width less-than (U+FF1C) and full-width greater-than (0xFF1E) signs: <>

    These Unicode characters do not require special encoding.

    0 讨论(0)
  • 2021-01-01 12:37

    You may try to use it like this:

    < = &lt;
    
    > = &gt;
    

    These are known as Character Entity References

    0 讨论(0)
提交回复
热议问题