XML转义字符\ x03

时间:2017-07-13 12:23:49

标签: python xml encoding lxml

我有一个 XML Exporter ,可以从我的数据库创建供稿,并且我有一个转义方法,这样我的数据的XML敏感字符就不会与XML标记冲突。

此方法如下所示:

/**
 * Main class of the application.
 */
public class Main{

  // Define the variable for the window of the game.
  public static JFrame window;

  // Define the variable for the introductory video.
  public static MediaPlayer video;

  // Define the variable for a thread.
  public static Thread thread;

  /**
   * Main function of the application.
   */
  public static void main(String[] args){

    // Create a Swing thread.
    SwingUtilities.invokeLater(new Runnable(){

      @Override
      public void run(){

        // Prevent the JavaFX toolkit from closing.
        Platform.setImplicitExit(false);

        // Create the window of the game.
        window = new JFrame();

        // Set the title.
        window.setTitle("Chip");

        // Set the resolution as 1920 x 1280.
        window.setSize(1926,1343);

        // Set the location as in the middle of the screen.
        window.setLocationRelativeTo(null);

        // Set the operation when the window closes.
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Disable the maximization and resizable mode.
        window.setResizable(false);

        // Show the window.
        window.setVisible(true);

        // Create a key listener.
        KeyListener keyListener = createKeyListener();

        // Create a JavaFX panel.
        JFXPanel panelJavaFX = new JFXPanel();

        // Add the key listener to the JavaFX panel.
        panelJavaFX.addKeyListener(keyListener);

        // Create a JavaFX thread.
        Platform.runLater(new Runnable(){

          @Override
          public void run(){

            // Create a new thread.
            thread = new Thread(new Runnable(){

              public void run(){

                try{

                  // Show the introductory video.
                  showVideo(panelJavaFX);

                  // Pause the execution of the application for 30 seconds (duration of the introductory video).
                  Thread.sleep(30000);

                }catch (InterruptedException interruptedException){

                  // Stop the video if an interruption has been occurred.
                  video.stop();

                }

                // Set the background image.
                String filename = "./media/image/background.jpg";
                window.setContentPane(new JLabel(new ImageIcon(filename)));

                // Show the window.
                window.setVisible(true);

                // Add the key listener to the window of the game.
                window.addKeyListener(keyListener);

              }

            });

            // Start the execution of the thread.
            thread.start();

          }

        });

      }

    });

  }

  /**
   * Creates a key listener.
   * @return Key listener.
   */
  public static KeyListener createKeyListener(){

    // Create the key listener.
    KeyListener keyListener = new KeyListener(){

      // Set the behavior whenever a key is pressed.
      @Override
      public void keyPressed(KeyEvent keyEvent){

        // Check if the "Escape" key is pressed.
        if (keyEvent.getKeyCode() == KeyEvent.VK_ESCAPE){

          // Check if the introductory video it is being played.
          if (video.getStatus().equals(Status.PLAYING)){

            // Make an interruption in the thread that is being executed.
            thread.interrupt();

          }

        }

      }

      // Set the behavior whenever a key is released.
      @Override
      public void keyReleased(KeyEvent keyEvent){}

      // Set the behavior whenever a key is typed.
      @Override
      public void keyTyped(KeyEvent keyEvent){}

    };

    // Return the key listener.
    return keyListener;

  }

  /**
   * Shows the introductory video.
   * @param panelJavaFX JFXPanel used to display the video.
   */
  public static void showVideo(JFXPanel panelJavaFX){

    // Set the size of the JaxaFX panel as the resolution of the introductory video (1920 x 1080).
    panelJavaFX.setSize(1920,1080);

    // Set the location of the JavaFX panel as in the middle of the window of the game.
    int coordinateX = (window.getWidth() - panelJavaFX.getWidth() - window.getInsets().left - window.getInsets().right) / 2;
    int coordinateY = (window.getHeight() - panelJavaFX.getHeight() - window.getInsets().top - window.getInsets().bottom) / 2;
    panelJavaFX.setLocation(coordinateX,coordinateY);

    // Define the video file.
    String filename = "./media/video/introduction.mp4";
    video = new MediaPlayer(new Media(new File(filename).toURI().toString()));

    // Add the video to the JavaFX panel.
    panelJavaFX.setScene(new Scene(new Group(new MediaView(video))));

    // Add the JavaFX panel to the window of the game.
    window.getContentPane().setLayout(null);
    window.add(panelJavaFX);

    // Play the video.
    video.setAutoPlay(true);

  }

}

我正在为此脚本使用LXML库,我遇到以下问题:

其中一个描述包含def escape(m_str): m_str = m_str.replace("&", "&amp;") m_str = m_str.replace("\n", "<br />") m_str = m_str.replace("<", "&lt;") m_str = m_str.replace(">", "&gt;") m_str = m_str.replace("\"", "&quot;") return m_str (不要问我为什么我们在描述中有这个角色,但我们有。)

对于更多视觉人士,以下是有问题描述的示例:

\x03

您可以看到第一个“空格”实际上不是空格而是to_be_escaped > 'gnebst G' [(x,ord(x)) for x in to_be_escaped] > <class 'list'>: [('g', 103), ('\x03', 3), ('n', 110), ('e', 101), ('b', 98), ('s', 115), ('t', 116), (' ', 32), ('G', 71)] 个字符(ref),第二个是“正常空格”(12月,{{3} })

问题是lxml对它的反应非常糟糕,这是代码:

End of text

输出(带有此字符):

  

PCDATA无效字符值3,第1行

我的问题是:

  • 当然,我可以扩展我的转义方法来解决问题,但是什么保证我不会发生另一个角色呢?
  • 我在哪里可以找到LXML中“禁止”字符的列表?
  • 其他人是否处理过这类问题,并作为一种适当的逃避方法(因为内置的方法并不比我的更好)?

1 个答案:

答案 0 :(得分:0)

我找到了一个答案的开头there(对于那个人的所有信用都是非常明确的解释)。

问题基本上是utf-8字符的映射在默认情况下不够好,我们需要指定源编码为utf8。

我们可以通过更改以下行来完成:

et.fromstring("<volltext>%s</volltext>" % cls.escape(job.description))

et.fromstring("<volltext>%s</volltext>" % cls.escape(job.description), parser=XMLParser(encoding='utf-8', recover=True))

为了更有弹性和健壮。