JSPM

@cwola/simple-xml-element

2.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q21022F
  • License MIT

JavaScript library, like PHP's SimpleXmlElement.

Package Exports

  • @cwola/simple-xml-element
  • @cwola/simple-xml-element/src/SimpleXmlElement.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@cwola/simple-xml-element) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

simple-xml-element

JavaScript library, like PHP's SimpleXmlElement.

Include

<script type="text/javascript" src="SimpleXmlElement.js"></script>

or

<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@cwola/simple-xml-element@${version}/src/SimpleXmlElement.js"></script>

Usage

const xmlString = `<Bookshelf>
<Book>
    <Name>The Wealth of Nations</Name>
    <Author>Adam Smith</Author>
    <Publishing>1776</Publishing>
</Book>
<Book>
    <Name>Crime and Punishment</Name>
    <Author>Fyodor Mikhaylovich Dostoyevsky</Author>
    <Publishing>1866</Publishing>
</Book>
<Book>
    <Name>And Then There Were None</Name>
    <Author>Agatha Christie</Author>
    <Publishing>1939</Publishing>
</Book>
</Bookshelf>`;
const xmlElement = simpleXmlLoadString(xmlString);

xmlElement.Bookshelf.Book.forEach((book) => {
    console.log(book.Name.$text() + ' (' + book.Author.$text() + ') - ' + book.Publishing.$text());
});
// The Wealth of Nations (Adam Smith) - 1776
// Crime and Punishment (Fyodor Mikhaylovich Dostoyevsky) - 1866
// And Then There Were None (Agatha Christie) - 1939


const newBook = xmlElement.Bookshelf.$addChild('Book');
newBook.$addChild('Name', 'A Clockwork Orange');
newBook.$addChild('Author', 'Anthony Burgess');
newBook.$addChild('Publishing', '1962');
console.log(xmlElement.$xpath('/Bookshelf/Book[last()]')[0].$asXML());
// <Book>
//     <Name>A Clockwork Orange</Name>
//     <Author>Anthony Burgess</Author>
//     <Publishing>1962</Publishing>
// </Book>

API

License

MIT