Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. QtXmlPatterns was removed from Qt6. How to use XPath in Qt6?

QtXmlPatterns was removed from Qt6. How to use XPath in Qt6?

Scheduled Pinned Locked Moved Solved Qt 6
5 Posts 3 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 8 Offline
    8 Offline
    8Observer8
    wrote on 24 Feb 2024, 08:45 last edited by 8Observer8
    #1

    Hi,

    I try to port my example from PySide2 to PySide6. What to use instead of QXmlQuery? I want to use XPath to parse animations from DAE COLLADA:

    from PySide6.QtXmlPatterns import QXmlQuery
    
            # ...
            path = "assets/armature.dae"
            if not f.open(QIODevice.OpenModeFlag.ReadOnly):
                print("Failed to load the file", path)
            query = QXmlQuery()
            query.bindVariable("file", f)
            query.setQuery("doc($file)//*:float_array[substring(@id, string-length(@id) - string-length('Bone_pose_matrix-input-array') + 1) = 'Bone_pose_matrix-input-array']/text()")
            self.times = query.evaluateToString().strip().split(" ")
            self.times = [float(i) for i in self.times]
    
            #...
    
    J 1 Reply Last reply 24 Feb 2024, 08:53
    1
    • 8 8Observer8
      24 Feb 2024, 08:45

      Hi,

      I try to port my example from PySide2 to PySide6. What to use instead of QXmlQuery? I want to use XPath to parse animations from DAE COLLADA:

      from PySide6.QtXmlPatterns import QXmlQuery
      
              # ...
              path = "assets/armature.dae"
              if not f.open(QIODevice.OpenModeFlag.ReadOnly):
                  print("Failed to load the file", path)
              query = QXmlQuery()
              query.bindVariable("file", f)
              query.setQuery("doc($file)//*:float_array[substring(@id, string-length(@id) - string-length('Bone_pose_matrix-input-array') + 1) = 'Bone_pose_matrix-input-array']/text()")
              self.times = query.evaluateToString().strip().split(" ")
              self.times = [float(i) for i in self.times]
      
              #...
      
      J Offline
      J Offline
      JonB
      wrote on 24 Feb 2024, 08:53 last edited by
      #2

      @8Observer8
      Sadly it's gone, and no replacement. See recent XPath 2.0 for Qt6 topic.

      But there the OP was able to "get libXML2 going". That has nothing to do with Qt, and isn't (directly) available from Python. Given Python, probably your only chance is to find an external non-Qt Python library which you can integrate.

      1 Reply Last reply
      2
      • 8 Offline
        8 Offline
        8Observer8
        wrote on 24 Feb 2024, 09:02 last edited by 8Observer8
        #3

        @JonB thanks a lot! It is sad that XPath was removed from Qt6. I what to have the same code base for C++ and Python. I think the best solution for me will be to use XML parsing directly without XPath like I used to parse vertices, normals, and texture coordinates:

        from PySide6.QtXml import QDomDocument
        
            # ...
        def initVertexBuffers(path: str):
               xmlDoc = QDomDocument()
            file = QFile(path)
            if not file.open(QIODevice.OpenModeFlag.ReadOnly):
                print("Failed to open the file: " + path)
            xmlDoc.setContent(file)
            file.close()
            
            vertPosArray = []
            normalArray = []
            texCoordArray = []
            indexArray = []
            
            root = xmlDoc.documentElement()
            daeElem = root.firstChildElement()
            while not daeElem.isNull():
                if daeElem.tagName() == "library_geometries":
                    geomElem = daeElem.firstChildElement()
                    if geomElem.tagName() == "geometry":
                        meshElem = geomElem.firstChildElement()
                        if meshElem.tagName() == "mesh":
                            meshChildElem = meshElem.firstChildElement()
                            while not meshChildElem.isNull():
                                floatArrayElem = meshChildElem.firstChildElement()
                                strArray = floatArrayElem.firstChild().toText().data().split(" ")
                                if meshChildElem.attribute("id").endswith("-mesh-positions"):
                                    vertPosArray = list(map(float, strArray))
                                if meshChildElem.attribute("id").endswith("-mesh-normals"):
                                    normalArray = list(map(float, strArray))
                                if meshChildElem.attribute("id").endswith("-mesh-map-0"):
                                    texCoordArray = list(map(float, strArray))
                                if meshChildElem.tagName() == "triangles" or meshChildElem.tagName() == "polylist":
                                    pChildElem = meshChildElem.firstChildElement()
                                    while not pChildElem.isNull():
                                        if pChildElem.tagName() == "p":
                                            strIndices = pChildElem.firstChild().toText().data().split(" ")
                                            indexArray = list(map(int, strIndices))
                                        pChildElem = pChildElem.nextSiblingElement()
                                meshChildElem = meshChildElem.nextSiblingElement()
                daeElem = daeElem.nextSiblingElement()
                # ...
        
        1 Reply Last reply
        0
        • 8 8Observer8 has marked this topic as solved on 24 Feb 2024, 09:03
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 24 Feb 2024, 10:15 last edited by
          #4

          Hi,

          You might want to check lxml2. It has a Python module so you should be able to re-use your code with ease.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          8 1 Reply Last reply 24 Feb 2024, 11:04
          1
          • S SGaist
            24 Feb 2024, 10:15

            Hi,

            You might want to check lxml2. It has a Python module so you should be able to re-use your code with ease.

            8 Offline
            8 Offline
            8Observer8
            wrote on 24 Feb 2024, 11:04 last edited by
            #5

            @SGaist thanks! It could be a good solution but for a while I don't know how to build external libraries for Android and WebAssembly. I want to be able to make build executables of my applications for Android, Desktop, and WebAssembly. It is why I like Qt very much because it has built-in libraries for parsing XML, JSON, drawing with build-in OpenGL ES, and WebSockets library to make multiplayer game clients for Android, Desktop, and Web. I want to host Node.js game servers with physics engine on free hosting render.com I don't have time to study how to build external libraries for Android and WebAssembly. I only know how to build OpenAL-Soft for Android to play 3D sounds and how to call Web Audio API from WebAssembly.

            1 Reply Last reply
            0

            1/5

            24 Feb 2024, 08:45

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved