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

How to handle values as string #597

Closed
5 of 6 tasks
waghcwb opened this issue Jul 7, 2023 · 4 comments
Closed
5 of 6 tasks

How to handle values as string #597

waghcwb opened this issue Jul 7, 2023 · 4 comments

Comments

@waghcwb
Copy link

waghcwb commented Jul 7, 2023

  • Are you running the latest version?
  • Have you included sample input, output, error, and expected output?
  • Have you checked if you are using correct configuration?
  • Did you try online tool?

Description

I want to know how to parse values as strings, because the parse is changing data in my XML

Code

async function Main() {
  let parser = new XMLParser()
  let object = parser.parse(`
    <?xml version="1.0"?>
    <any_name>
        <person>
            <phone>600.00</phone>
        </person>
    </any_name>
  `)
  console.info(object)
  // { '?xml': '', any_name: { person: { phone: 600 } } }
}

Output

{ '?xml': '', any_name: { person: { phone: 600 } } }

expected data

{ '?xml': '', any_name: { person: { phone: '600.00' } } }

Would you like to work on this issue?

  • Yes
  • No
@github-actions
Copy link

github-actions bot commented Jul 7, 2023

We're glad you find this project helpful. We'll try to address this issue ASAP. You can vist https://solothought.com to know recent features. Don't forget to star this repo.

@amitguptagwl
Copy link
Member

Please check strnum option and library for appropriate configuration.

@cecia234
Copy link
Contributor

cecia234 commented Jul 19, 2023

Hi, you can set parseTagValue: false inside the options to avoid having strnum parse the value for you.

let options =  {
    parseTagValue: false,
}
let parser = new XMLParser(options)
let object = parser.parse(`
    <?xml version="1.0"?>
    <any_name>
        <person>
            <phone>600.00</phone>
        </person>
    </any_name>
`)
console.info(object)
// { '?xml': '', any_name: { person: { phone: '600.00' } } }

You can also specify a custom tag parser inside the options object (tagValueProcessor property) to handle different cases based on your needs:

let options =  {
    parseTagValue: false,
    tagValueProcessor: (tagName, tagValue) => {
        if (typeof tagValue === 'string') {
            return "HELLO!";
        } else if (tagValue === undefined || tagValue === null) {
            return null;
        }
        return tagValue.toString();
    }
}
let parser = new XMLParser(options)
let object = parser.parse(`
    <?xml version="1.0"?>
    <any_name>
        <person>
            <phone>600.00</phone>
        </person>
    </any_name>
  `)
console.info(object)
// { '?xml': '', any_name: { person: { phone: "HELLO!" } } }

@amitguptagwl
Copy link
Member

I hope the issue is not anymore, or you can start the discussion thread.

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

3 participants