Categories

.xml()

Categories: DOM Insertion, Inside | Attributes

.xml()Returns: String

Plugin: jQuery.xml

Description: Get the XML contents of the first element in the set of matched elements.

  • .xml()

    version added: 1.0

The .xml() method is only available for XML documents, not HTML or XHTML.

We can use .xml() to get the contents of any element. If the selector expression matches more than one element, only the first one's XML content is returned.

  • Get XML from first element containing the class 'inner'

    XML:
    <?xml version="1.0" encoding="utf-8"?>
    <root>
      <entry class="inner">
        Hello
        <test>Regards</test>
      </entry>
      <entry class="inner">
        Goodbye
        <test>Regards</test>
      </entry>
    </root>
    Code:
    $( '.inner', xmlDocument ).xml();
    Results:
    Hello
    <test>Regards</test>

.xml( xmlString )Returns: jQuery

Plugin: jQuery.xml

Description: Set the XML contents of each element in the set of matched elements.

  • .xml( xmlString )

    version added: 1.0

    xmlString   A string of XML to set as the content of each matched element.

  • .xml( function(index, oldxml) )

    version added: 1.0

    function(index, oldxml)   A function returning the XML content to set. Receives the index position of the element in the set and the old XML value as arguments. jQuery empties the element before calling the function; use the oldxml argument to reference the previous content.

The .xml() method is only available for XML documents, not HTML or XHTML.

When we use .xml() to set elements' contents, any contents that were in those elements is completely replaced by the new contents.

  • Set some XML to each element containing the class 'inner'.

    XML:
    <?xml version="1.0" encoding="utf-8"?>
    <root>
      <entry class="inner">
        Hello
      </entry>
      <entry class="inner">
        Goodbye
      </entry>
    </root>
    Code:
    $( '.inner', xmlDocument )
        .xml('<test>Regards</test>');
    // Each inner <entry> element content will replace with this new content
    Results:
    <?xml version="1.0" encoding="utf-8"?>
    <root>
      <entry class="inner">
        <test>Regards</test>
      </entry>
      <entry class="inner">
        <test>Regards</test>
      </entry>
    </root>