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 put the text in the front of a tag when parse a xml? #669

Closed
wxfred opened this issue Aug 22, 2024 · 6 comments
Closed

How to put the text in the front of a tag when parse a xml? #669

wxfred opened this issue Aug 22, 2024 · 6 comments

Comments

@wxfred
Copy link

wxfred commented Aug 22, 2024

Input

const xml = `
<a>
  some text here
  <c attr="xxx"></c>
  <b attr="xxx"></b>
</a>
`

Output by

const parser = new XMLParser({
  ignoreAttributes: false,
  attributeNamePrefix: '',
  textNodeName : 'text',
})
const xmlObj = parser.parse(xml)
console.log(xmlObj)
// Output 1
{
 a : {
    c: { attr: 'xxx' },
    b: { attr: 'xxx' },
    text: 'some text here',
  }
}

Desired output

// Output 2
{
 a : {
    text: 'some text here',
    c: { attr: 'xxx' },
    b: { attr: 'xxx' },
  }
}

If i rebuild the output,

const builder = new XMLBuilder({
  ignoreAttributes: false,
  attributeNamePrefix: '',
  textNodeName : 'text',
  format: true,
  indentBy: '    ',
})
console.log(builder.build(xmlObj))

the rebuild result of Output 1 is

<a>
    <c attr="xxx"></c>
    <b attr="xxx"></b>
 some text here</a>

the rebuild result of Output 2 is below, and the order of tag's text is the same as the Input, this is what i want

<a>
some text here    <c attr="xxx"></c>
    <b attr="xxx"></b>
</a>
Copy link

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

do you mean the indentation of tag's text is not as per expectation?

@wxfred wxfred changed the title How to put the text in the front of a tag when build xml? How to put the text in the front of a tag when parse a xml? Aug 26, 2024
@wxfred
Copy link
Author

wxfred commented Aug 26, 2024

@amitguptagwl Sorry, I make a mistake. The builder can build tag's text in right order, what i want is changing the parse order of tag's text, so the builder can build the text before the child tag. I have changed the title and description of this topic, pls have a look.

@amitguptagwl
Copy link
Member

@wxfred
Copy link
Author

wxfred commented Aug 31, 2024

@amitguptagwl When preserveOrder: true , you get some long ugly result.
The result is ugly, it is not convenient to add something or modify something.

@amitguptagwl
Copy link
Member

@wxfred In JS, you can ensure the sequence of properties. This is not the issue with this library. Hence, that;s the only solution.

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