Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect parsing of xml with comments #54

Closed
RShabanov opened this issue Jun 26, 2023 · 1 comment · Fixed by #55
Closed

Incorrect parsing of xml with comments #54

RShabanov opened this issue Jun 26, 2023 · 1 comment · Fixed by #55

Comments

@RShabanov
Copy link

RShabanov commented Jun 26, 2023

Hello everyone! If I try to parse an xml string with comments, it doesn't work correctly for some kml tags. I assume it doesn't work correctly for those tags that are read in methods with _ => break in the scope of the loop. For example, in KmlReader::read_location

loop {
    let mut e = self.reader.read_event_into(&mut self.buf)?;
    match e {
        Event::Start(ref mut e) => match e.local_name().as_ref() {
            b"longitude" => longitude = self.read_float()?,
            b"latitude" => latitude = self.read_float()?,
            b"altitude" => altitude = self.read_float()?,
            _ => {}
        },
        Event::End(ref mut e) => {
            if e.local_name().as_ref() == b"Location" {
                break;
            }
        }
        _ => break, // it breaks even if it is a comment tag
    }
}

If you try to parse, for instance, such kml string

<Location>
  <!-- my comment -->
  <longitude>39.55375305703105</longitude> 
  <latitude>-118.9813220168456</latitude> 
  <altitude>1223</altitude>
</Location>

You will get this

KmlDocument(
    KmlDocument {
        version: Unknown,
        attrs: {},
        elements: [
            Location(
                Location {
                    latitude: 0.0,
                    longitude: 0.0,
                    altitude: 0.0,
                    attrs: {},
                },
            ),
            Element(
                Element {
                    name: "longitude",
                    attrs: {},
                    content: Some(
                        "39.55375305703105",
                    ),
                    children: [],
                },
            ),
            Element(
                Element {
                    name: "latitude",
                    attrs: {},
                    content: Some(
                        "-118.9813220168456",
                    ),
                    children: [],
                },
            ),
            Element(
                Element {
                    name: "altitude",
                    attrs: {},
                    content: Some(
                        "1223",
                    ),
                    children: [],
                },
            ),
        ],
    },
)

When it is expected to be something like

Location(
    Location {
        latitude: -118.9813220168456,
        longitude: 39.55375305703105,
        altitude: 1223.0,
        attrs: {},
    },
)
@pjsier
Copy link
Member

pjsier commented Jun 28, 2023

@RShabanov thanks for the issue! This is a great catch, and it should be a simple fix. I can get started on that now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants