Python reads xml from entry to proficiency

 

XML (Extensible Markup Language), Extensible Markup Language, is a file format widely used on the Internet. On the Internet, information in web pages is stored in XML format, such as HTML, CSV, JSON, etc. With the development of e-commerce, people need to transmit various information on Web pages, so a simple XML file is needed to carry these information. Since XML is a standard data format, the same type of data can be easily shared between different companies or individuals. XML is a common format for various web applications. It provides a common representation for all types of data. It sends information to the web server without any additional processing. However, since XML is an unstructured file, it cannot be read directly. Currently, there are two processing methods for XML format files: 1. Using xml to parse XML files. For example, use sublime text, csharedt, etc. to parse XML files; for example, use parsers such as xslmx; and this article will introduce how to use python to read XML format files

  • 1. Preparations

    1.1 First of all, we need to prepare a tool that needs to be used: python. Because our goal is to read XML format files, we need to use python to write corresponding programs. Python has a wealth of third-party libraries and can run well on various platforms, such as Windows, Linux, and Mac OS. Below is an example where there are xml, json, list etc files and we need to convert them into xml files. 1.2 Secondly, we need to prepare the following files: docx: used to store the metadata information of the XML file, such as XML type text, file name, attributes, etc. Since this docx file is a binary file, it can be read directly using python. txt: used to store document information in XML format. But if we want to read the entire document, we need to use the document in txt format. This is because when we read the XML format document, it will be converted into a json format data file. The corresponding python code is included in both docx and txt. test: It is used to test whether the program can run normally, which is what we must do when using python to read XML format files. Also, it can test the program for bugs or other problems. In addition, it can also be used to test whether the program can run normally on different browsers.

  • 2. Create an xml file

    First create a file named "", eg "".xml. When creating this file, save the XML document as a file named ".xml". This file can be used to read, store and process XML documents, and can also be used when using xml to parse XML files. When using an xml parser, you need to save the XML document as a file named ". xml" before you can use xml to parse the XML document. Then use xslmx (or other parser) to parse the file into corresponding XML data. For programs using sublime text, since sublime text does not support the reading and saving of XML documents, it is necessary to use an interpreter called pypy to read and save XML documents.

  • 3. Read data

    When we need to read data from an XML file, we can use python's bytecode() function. It should be noted here that the data is not necessarily all the content in the XML file. From this point, we can use the readxml() function to read the specified XML file, then use the bytecode() function to decompress the data, convert it, and finally store the result in a new XML file. Let's take the following example as an example to explain how to do it. The result of running the following code is as follows: As you can see, the above code outputs an XML file after running. However, there is no data in the XML file. Therefore, we need to perform further operations on the XML file. First of all, we need to read data from the root directory (folder path), so as to ensure that we will not affect this XML file when we perform other operations. As you can see, there is no data in the root directory. So we need to use the bootstrap.xml() function to get the data in the root directory. Next, we read the first data in the XML file into a new xml file. This read operation uses the setReadById() function to read the first value in the xml file. Next, we operate on the xml file: As you can see, after the above operations, the XML file already has data. Next, we need to further process this data and convert it to text format. Because different browsers have different levels of support for text formats, corresponding tests need to be carried out before use to ensure that they can be displayed normally. First convert the XML file to a string format and then parse it using the char() function in python. As you can see, the data has been converted to text format. Next we can further process the text:

  • 4. Write out the data

    First of all, let's take a look at the structure of the file: This is the data we want to read, we need to process it and write it out. So I put a row of data into a variable, as follows: In Python, we use the py_install() function to specify the location of the variable. In the py_install() function, we pass this variable as an argument to a variable. We want this variable to be accessed by only one program. Then, all we have to do is write this variable to the document.xml file. Below I will use a simple example to illustrate how to read the xml file: Next I will use the following code to complete this step: In the above code, I used a simple way to process the data. However, when I'm working with the data, I want it to contain some information. At this time, I used the document.xml file. In the above code, I put a variable into a file named document.xml by using the py_install() function.

  • 5. Running results

    First, we use python to call the textarea function in the numpy library. You can see that the function returns a Numpy object, which contains all the information in the numpy. dom file, including: 1. xml file: 3. in numpy. dom Elements: We can see from the above code that this function returns a file containing all the information of the Numpy object, including a collection of all the data in the numpy. dom file. Therefore, when using Python to read an XML format file, it is necessary to first parse the XML file into an xml document. In addition, although we can convert a string into a Numpy object, we can also convert a Numpy object into an object in another format (such as zip format) for reading.

  • Commonly used python reads xml code:

  • 1. Use the ElementTree module to read the XML file:
    ```python
    import xml.etree.ElementTree as ET
    # Open the XML file
    tree = ET.parse('example.xml')
    # Get the root node
    root = tree.getroot()
    # Traverse the XML file
    for child in root:
    print(child.tag, child.attrib)
    for sub_child in child:
    print(sub_child.tag, sub_child.text)
    ```
    2. Use the minidom module to read the XML file:
    ```python
    import xml.dom.minidom as minidom
    # Open the XML file
    dom = minidom.parse('example.xml')
    # Get the root node
    root = dom.documentElement
    # Traverse the XML file
    for child in root.childNodes:
    if child.nodeType == child.ELEMENT_NODE:
    print(child.tagName, child.attributes.items())
    for sub_child in child.childNodes:
    if sub_child.nodeType == sub_child.ELEMENT_NODE:
    print(sub_child.tagName, sub_child.firstChild.data)
    ```
    3. Use the lxml module to read the XML file:
    ```python
    from lxml import etree
    # Open the XML file
    tree = etree.parse('example.xml')
    # Get the root node
    root = tree.getroot()
    # Traverse the XML file
    for child in root:
    print(child.tag, child.attrib)
    for sub_child in child :
    print(sub_child.tag, sub_child.text)
    ```

Guess you like

Origin blog.csdn.net/qq_42751978/article/details/129897333
Recommended