0

I'm trying to extract a list of X,Y,Z from xml file. A part of xml is:

 <Data>
      <TargetPosition X="57.23471787820652" Y="-26.04271457691532" 
Z="9.988092935592704" Valid="1"/> #PosLang
      <StartPosition X="0" Y="0" Z="0" Valid="0"/>
    </Data>
  </Object>
  <Object Type="{aa99a9ec-4b85-442e-b914-de3579656eb5}">
    <ParentTObject Valid="1">
      <Translation X="0" Y="0" Z="0"/>
      <Rotation W="1" X="0" Y="0" Z="0"/>
    </ParentTObject>
    <Data>
      <TargetPosition X="58.81901290773406" Y="-20.09883392050945" 
Z="16.53197054898237" Valid="1"/> #NegLang
      <StartPosition X="0" Y="0" Z="0" Valid="0"/>
    </Data>
  </Object>

I need to extract X,Y,Z from all TargetPosition in file that have #PosLang comment

def targets(path='some.xml'):
    try:
        e = ET.parse(path).getroot()
    except FileNotFoundError:
        return list()

    Position = namedtuple('float', ['x', 'y', 'z'])

    for position in e.iter('TargetPosition'):
        yield Position(
            x=float(position.get('X')), 
            y=float(position.get('Y')),
            z=float(position.get('Z'))
        )

In y code i extract X,Y,Z of all TargetPosition, but i need only that have #PosLang comment

1
  • Don't confuse Python comments using # and XML comments. Commented Apr 24, 2019 at 13:00

2 Answers 2

4

If your XML really contains the string #PosLang in exactly the way your sample shows, then that's not a comment, but a regular text node.

And since that text node follows the <TargetPosition> element, it will be in the .tail property:

def targets(path='some.xml'):
    try:
        e = ET.parse(path).getroot()
    except FileNotFoundError:
        return list()

    Position = namedtuple('float', ['x', 'y', 'z'])

    for position in e.iter('TargetPosition'):
        if "#PosLang" in position.tail:
            yield Position(
                x=float(position.get('X')), 
                y=float(position.get('Y')),
                z=float(position.get('Z'))
            )
Sign up to request clarification or add additional context in comments.

2 Comments

Exception appears: if "#PosLang" in position.tail: ^ TabError: inconsistent use of tabs and spaces in indentation
You seem to be using tabs to indent your Python source code. Don't do that. The convention is that Python indentation is four spaces - update your editor's settings, make whitespace visible, fix the errors in your code indentation.
0

As per the docs, ElementTree ignores XML comments. Can you get the XML generated differently?

3 Comments

The problem is that it should be commented after generated. Because it should be marked as "negative" or "positive"
What is the criterion? When is a position "postive" and when is it "negative"?
Heuristicly, xml comes from neuronavigation system, it only produces points, without labeling them, if there was “language” response from person it will be positive, visa-versa

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.