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

Multiple child tags are created while converting to xml #696

Open
vigneshsdev opened this issue Sep 1, 2023 · 1 comment
Open

Multiple child tags are created while converting to xml #696

vigneshsdev opened this issue Sep 1, 2023 · 1 comment

Comments

@vigneshsdev
Copy link

I have two arrays in a json, which I need to convert to xml. Below is my code

var xml2js = require("xml2js");

let input = {
  cities: [
    {
      position: {
        Longitude: 9.96233,
        Latitude: 49.80404,
      },
    },
    {
      position: {
        Longitude: 6.80592,
        Latitude: 51.53548,
      },
    },
  ],
  states: [
    {
        position: {
          Longitude: 9.96233,
          Latitude: 49.80404,
        },
      },
      {
        position: {
          Longitude: 6.80592,
          Latitude: 51.53548,
        },
      },
  ]
};

const builder = new xml2js.Builder({ headless: true });
const result = builder.buildObject(input);
console.log(result);

Obtained Result:

<root>
  <cities>
    <position>
      <Longitude>9.96233</Longitude>
      <Latitude>49.80404</Latitude>
    </position>
  </cities>
  <cities>
    <position>
      <Longitude>6.80592</Longitude>
      <Latitude>51.53548</Latitude>
    </position>
  </cities>
  <states>
    <position>
      <Longitude>9.96233</Longitude>
      <Latitude>49.80404</Latitude>
    </position>
  </states>
  <states>
    <position>
      <Longitude>6.80592</Longitude>
      <Latitude>51.53548</Latitude>
    </position>
  </states>
</root>

Expected Result:

<root>
  <cities>
    <position>
      <Longitude>9.96233</Longitude>
      <Latitude>49.80404</Latitude>
    </position>
    <position>
      <Longitude>6.80592</Longitude>
      <Latitude>51.53548</Latitude>
    </position>
  </cities>
  <states>
    <position>
      <Longitude>9.96233</Longitude>
      <Latitude>49.80404</Latitude>
    </position>
    <position>
      <Longitude>6.80592</Longitude>
      <Latitude>51.53548</Latitude>
    </position>
  </states>
</root>
@urbancamo
Copy link

urbancamo commented Jan 29, 2024

I have a similar issue, given the JSON structure:

{
  "bookId": "35972826-85ec-4877-99dd-32a9f14587ec",
  "bookTitle": "The Vietnam War",
  "parts": [
    {
      "partTitle": "The Genesis of Conflict",
      "chapters": [
        {
          "chapterTitle": "Colonial Tensions and the Rise of Ho Chi Minh",
          "sections": [
            {
              "sectionTitle": "The Seeds of Rebellion",
              "paragraphs": [
                {
                  "paragraphText": "EDITED CHAPTER CONTENT In exploring the spirit of early Vietnamese resistance against the French, one poignant anecdote revolves around the legendary Trung Sisters, Trung Trac, and Trung Nhi. In the first century AD, they embodied the fervent resistance against foreign domination. Facing the oppressive rule of the Chinese Han Dynasty, the Trung Sisters rallied their fellow Vietnamese in a daring revolt. Their leadership and courage inspired a formidable army that successfully expelled the Chinese forces, declaring a brief but significant period of Vietnamese independence."
                },
                {
                  "paragraphText": "The Trung Sisters' tale resonates as a powerful symbol of unwavering determination against external forces. Their commitment to liberating their homeland became a rallying cry for future generations of Vietnamese resisting foreign dominance. This early episode laid a foundation for the enduring spirit of resistance that continued to shape Vietnam's struggle against various colonizers, including the French in the subsequent centuries."
                },
                {
                  "paragraphText": "The Trung Sisters' legacy serves as a testament to the indomitable spirit that fueled early Vietnamese resistance, setting a precedent for future generations in their pursuit of sovereignty and national identity. Their story is woven into the fabric of Vietnamese history, embodying the resilience and unity that became hallmarks of the ongoing struggle against foreign incursions."
                },
                {
                  "paragraphText": "In examining the American Vietnam War and drawing parallels to modern times, one might find a modern-day equivalent to the economic and social pressures faced by the Vietnamese under French colonial rule in the challenges experienced by nations grappling with neocolonial economic structures. Neocolonialism refers to the indirect economic and cultural influence that former colonial powers maintain over their former colonies, often perpetuating economic imbalances and exploitation."
                },

I get the following result:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
  <bookId>35972826-85ec-4877-99dd-32a9f14587ec</bookId>
  <bookTitle>The Vietnam War</bookTitle>
  <parts>
    <partTitle>The Genesis of Conflict</partTitle>
    <chapters>
      <chapterTitle>Colonial Tensions and the Rise of Ho Chi Minh</chapterTitle>
      <sections>
        <sectionTitle>The Seeds of Rebellion</sectionTitle>
        <paragraphs>
          <paragraphText>EDITED CHAPTER CONTENT In exploring the spirit of early Vietnamese resistance against the French, one poignant anecdote revolves around the legendary Trung Sisters, Trung Trac, and Trung Nhi. In the first century AD, they embodied the fervent resistance against foreign domination. Facing the oppressive rule of the Chinese Han Dynasty, the Trung Sisters rallied their fellow Vietnamese in a daring revolt. Their leadership and courage inspired a formidable army that successfully expelled the Chinese forces, declaring a brief but significant period of Vietnamese independence.</paragraphText>
        </paragraphs>
        <paragraphs>
          <paragraphText>The Trung Sisters' tale resonates as a powerful symbol of unwavering determination against external forces. Their commitment to liberating their homeland became a rallying cry for future generations of Vietnamese resisting foreign dominance. This early episode laid a foundation for the enduring spirit of resistance that continued to shape Vietnam's struggle against various colonizers, including the French in the subsequent centuries.</paragraphText>
        </paragraphs>
        <paragraphs>
          <paragraphText>The Trung Sisters' legacy serves as a testament to the indomitable spirit that fueled early Vietnamese resistance, setting a precedent for future generations in their pursuit of sovereignty and national identity. Their story is woven into the fabric of Vietnamese history, embodying the resilience and unity that became hallmarks of the ongoing struggle against foreign incursions.</paragraphText>
        </paragraphs>
        <paragraphs>
          <paragraphText>In examining the American Vietnam War and drawing parallels to modern times, one might find a modern-day equivalent to the economic and social pressures faced by the Vietnamese under French colonial rule in the challenges experienced by nations grappling with neocolonial economic structures. Neocolonialism refers to the indirect economic and cultural influence that former colonial powers maintain over their former colonies, often perpetuating economic imbalances and exploitation.</paragraphText>
        </paragraphs>

when I expected all <paragraphText> nodes to be contained in one <paragraphs> node.

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