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

Not all attributes of tag in .attributes() #299

Closed
swi2012 opened this issue Jul 23, 2021 · 5 comments
Closed

Not all attributes of tag in .attributes() #299

swi2012 opened this issue Jul 23, 2021 · 5 comments

Comments

@swi2012
Copy link

swi2012 commented Jul 23, 2021

I'm trying quick-xml and i found a strnage behavior on my xml file (a sample attached). When i parse event Begin on tag SECURITY i get only two first attributes. But on tag RECORDS i get all attributes successfully.
My code like

Ok(Event::Start(ref e)) => {
                        if let b"SECURITY" = e.name() {
                            // cycle through attrs
                            for el in e.attributes() {
                                let e = el?;
                                print!("{}, ", std::str::from_utf8(e.key)?);
                            }
                            println!();
                        };
[1.xml.gz](https://github.com/tafia/quick-xml/files/6868035/1.xml.gz)
@tafia
Copy link
Owner

tafia commented Aug 10, 2021

I can't get your file, are you still having some issue?

@swi2012
Copy link
Author

swi2012 commented Aug 10, 2021

Hello. Yes issue is still here.
Here is file content

<?xml version="1.0" encoding="windows-1251"?>
<MICEX_DOC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <SECURITY SecurityId="PLZL" ISIN="RU000A0JNAA8" SecShortName="Short Name" PriceType="CASH">
    <RECORDS RecNo="1" TradeNo="1111" TradeDate="2021-07-08" TradeTime="15:00:00" BuySell="S" SettleCode="Y1Dt" Decimals="3" Price="13057.034" Quantity="766" Value="10001688.29" AccInt="0" Amount="10001688.29" Balance="766" TrdAccId="X0011" ClientDetails="2222" CPFirmId="3333" CPFirmShortName="Firm Short Name" Price2="13057.034" RepoPart="2" ReportTime="16:53:27" SettleTime="17:47:06" ClientCode="4444" DueDate="2021-07-09" EarlySettleStatus="N" RepoRate="5.45" RateType="FIX"/>
  </SECURITY>
</MICEX_DOC>

@tafia
Copy link
Owner

tafia commented Aug 11, 2021

I haven't tried but your encoding is windows-1251 not utf8, could you try using unescape_and_decode ?

@swi2012
Copy link
Author

swi2012 commented Aug 11, 2021

I use feature 'encoding'. Even so with utf8 file it the same:

<?xml version="1.0" encoding="utf-8"?>
<MICEX_DOC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <SECURITY SecurityId="PLZL" ISIN="RU000A0JNAA8" SecShortName="Short Name" PriceType="CASH">
    <RECORDS RecNo="1" TradeNo="1111" TradeDate="2021-07-08" TradeTime="15:00:00" BuySell="S" SettleCode="Y1Dt" Decimals="3" Price="13057.034" Quantity="766" Value="10001688.29" AccInt="0" Amount="10001688.29" Balance="766" TrdAccId="X0011" ClientDetails="2222" CPFirmId="3333" CPFirmShortName="Firm Short Name" Price2="13057.034" RepoPart="2" ReportTime="16:53:27" SettleTime="17:47:06" ClientCode="4444" DueDate="2021-07-09" EarlySettleStatus="N" RepoRate="5.45" RateType="FIX"/>
  </SECURITY>
</MICEX_DOC>

output:
read file 1.xml reader opened some SECURITY: SecurityId, ISIN, RECORDS: RecNo,TradeNo,TradeDate,TradeTime,BuySell,SettleCode,Decimals,Price,Quantity,Value,AccInt,Amount,Balance,TrdAccId,ClientDetails,CPFirmId,CPFirmShortName,Price2,RepoPart,ReportTime,SettleTime,ClientCode,DueDate,EarlySettleStatus,RepoRate,RateType,

@tafia
Copy link
Owner

tafia commented Aug 14, 2021

Hi,
I've tried this test and it seems to work as expected. Closing for now, feel free to reopen and provide more information.

#[test]
fn test_issue299() -> Result<(), Error> {
    let xml = r#"
<?xml version="1.0" encoding="utf8"?>
<MICEX_DOC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <SECURITY SecurityId="PLZL" ISIN="RU000A0JNAA8" SecShortName="Short Name" PriceType="CASH">
    <RECORDS RecNo="1" TradeNo="1111" TradeDate="2021-07-08" TradeTime="15:00:00" BuySell="S" SettleCode="Y1Dt" Decimals="3" Price="13057.034" Quantity="766" Value="10001688.29" AccInt="0" Amount="10001688.29" Balance="766" TrdAccId="X0011" ClientDetails="2222" CPFirmId="3333" CPFirmShortName="Firm Short Name" Price2="13057.034" RepoPart="2" ReportTime="16:53:27" SettleTime="17:47:06" ClientCode="4444" DueDate="2021-07-09" EarlySettleStatus="N" RepoRate="5.45" RateType="FIX"/>
  </SECURITY>
</MICEX_DOC>
"#;
    let mut reader = Reader::from_str(xml);
    loop {
        match reader.read_event_unbuffered()? {
            Start(e) | Empty(e) => {
                let attr_count = match e.name() {
                    b"MICEX_DOC" => 1,
                    b"SECURITY" => 4,
                    b"RECORDS" => 26,
                    _ => unreachable!(),
                };
                assert_eq!(
                    attr_count,
                    e.attributes().filter(Result::is_ok).count(),
                    "mismatch att count on '{}'",
                    reader.decoder().decode(e.name())?
                );
            }
            Eof => break,
            _ => (),
        }
    }
    Ok(())
}

(I'll add it to the tests).

@tafia tafia closed this as completed Aug 14, 2021
Mingun added a commit to Mingun/quick-xml that referenced this issue Sep 29, 2023
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

No branches or pull requests

2 participants