Xmldocument To String Formatted Bibliography

At times, you may need to parse XML content and convert it into a DOM tree, or, conversely, serialize an existing DOM tree into XML. In this article, we'll look at the objects provided by the web platform to make the common tasks of serializing and parsing XML easy.

Serializes DOM trees, converting them into strings containing XML.
Constructs a DOM tree by parsing a string containing XML, returning a or as appropriate based on the input data.
Loads content from a URL; XML content is returned as an XML object with a DOM tree built from the XML itself.
XPath
A technology for creating strings that contain addresses for specific portions of an XML document, and locating XML nodes based on those addresses.

Creating an XML document

Using one of the following approaches to create an XML document (which is an instance of .

Parsing strings into DOM trees

This example converts an XML fragment in a string into a DOM tree using a :

var sMyString = '<a id="a"><b id="b">hey!</b></a>'; var oParser = new DOMParser(); var oDOM = oParser.parseFromString(sMyString, "text/xml"); // print the name of the root element or error message console.log(oDOM.documentElement.nodeName == "parsererror" ? "error while parsing" : oDOM.documentElement.nodeName);

Parsing URL-addressable resources into DOM trees

Using XMLHttpRequest

Here is sample code that reads and parses a URL-addressable XML file into a DOM tree:

var xhr = new XMLHttpRequest(); xhr.onload = function() { dump(xhr.responseXML.documentElement.nodeName); } xhr.onerror = function() { dump("Error while getting XML."); } xhr.open("GET", "example.xml"); xhr.responseType = "document"; xhr.send();

The value returned in the object's field is a constructed by parsing the XML.

If the document is HTML, the code shown above will return a . If the document is XML, the resulting object is actually a . The two types are essentially the same; the difference is largely historical, although differentiating has some practical benefits as well.

Note: There is in fact an interface as well, but it is not necessarily an independent type. In some browsers it is, while in others it is simply an alias for the interface.

Serializing an XML document

Given a , you can serialize the document's DOM tree back into XML using the method.

Use the following approaches to serialize the contents of the XML document you created in the previous section.

Serializing DOM trees to strings

First, create a DOM tree as described in How to Create a DOM tree. Alternatively, use a DOM tree obtained from .

To serialize the DOM tree into XML text, call :

var oSerializer = new XMLSerializer(); var sXML = oSerializer.serializeToString(doc);

Serializing HTML documents

If the DOM you have is an HTML document, you can serialize using , but there is a simpler option: just use the property (if you want just the descendants of the specified node) or the property if you want the node and all its descendants.

var docHTML = document.documentElement.innerHTML;

As a result, is a containing the HTML of the contents of the document; that is, the element's contents.

You can get HTML corresponding to the and its descendants with this code:

var docHTML = document.documentElement.outerHTML;

See also

Document Tags and Contributors

 Contributors to this page:Sheppy, sideshowbarker, mikerissi, tonyfinn, Mkmelin, RomainLanz, rolfedh, southpolesteve, teoli, kscarfone, Tim_Morgan, gal007, ethertank, fusionchess, ziyunfei, Hsivonen, pincopalla, akorchev, kmaglione, Reno Greenleaf, lgreco, Brettz9, Kirschkern, ericjung, Mgjbot, Nickolay, IsNull, FST777, Nightstrike

 Last updated by:Sheppy,

Office has a deeper integration with XML technology and developers are always looking for tips and tricks to work with XML documents. Office provides support to work with XML and you might be one of those developers that is programmatically generating Word documents (using WordprocessingML) , Excel spreadsheets (using SpreadsheetML), PowerPoint slides (using PresentationML), or Visio diagrams using (DataDiagrammingML). I think it always comes handy to have a list of tips and tricks to work with XML, and today I will share with you three simple ways of indenting an XML file/document.


Indenting XML files might sound as one of those netpick or nice-to-have enhancements that you don’t really need when you are working with XML. However, lots of applications and tools generate programmatically XML files and it always comes handy to open a nice and readable indented XML file instead of a “how can I edit this!” single line of eternal XML elements.


For managed applications:



  • If you are generating XML files using and XmlTextWriter, you just need to do the following:


    [VB.NET]


    [C#]



  • If you are generating XML files/documents using DOM (XmlDocument) , you can add an XmlTextWriter to indent the code and you will be done:


    [VB.NET]


    [C#]


For any platform:



  • If you are generating XML files using an XSL transform file, you just need to add a simple line to your XSL file.




Happy Office XML programming!


0 comments

Leave a Reply

Your email address will not be published. Required fields are marked *