Skip to content
Advertisement

How to parse xml from local file or url with lxml?

I try to use lxml to parse xml, but I have a problem:

JavaScript

Here is my code:

JavaScript

I’m a newbie on lxml. Please help me to fix this issue. There is my xml content

JavaScript

One more, could we have parse xml from url with lxml.

Thanks & Best Regards,

Advertisement

Answer

The reason you are getting the error message invalid x escape is that you are using etree.fromstring() to attempt to load XML from a file. This function is used to load XML directly from a string, and you are passing it a path with in it.

In effect the function is trying to parse your file path as XML. The path contains escape characters with invalid characters following them (i.e. n would be a valid newline)

To load your XML from a file, you need to use the etree.parse() function as follows:

JavaScript

When passing file paths to Python functions, you should normally prefix your string with r to tell Python not to try and escape the characters inside your path. For example c:temp would actually result in passing c:<tab character>emp, i.e. the t is converted into a tab character. Adding the r to the start stops this happening.

Alternatively you can pass the path as follows:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement