10 Easy Steps to Read XML Files

10 Easy Steps to Read XML Files

XML (Extensible Markup Language) recordsdata are a strong and versatile knowledge format utilized in numerous functions. Whether or not you are a seasoned developer or a novice, mastering the artwork of studying XML recordsdata is a basic talent within the digital age. On this complete information, we’ll delve into the intricacies of XML, offering you with the information and strategies you might want to navigate the huge world of XML knowledge with ease.

At its core, XML is a self-describing knowledge format that makes use of tags to outline the construction and content material of information. This hierarchical construction permits for the group of complicated info in a way that is each human and machine-readable. By leveraging this structured format, you possibly can effortlessly extract and manipulate knowledge from XML recordsdata, making them an indispensable instrument for knowledge change and processing.

Reading XML files

Moreover, the flexibility of XML extends to a variety of functions, together with net providers, configuration recordsdata, and knowledge storage. Its flexibility permits for the customization of tags and attributes to go well with particular wants, making it a extremely adaptable knowledge format for numerous domains. Whether or not you are working with knowledge in healthcare, finance, or another trade, XML supplies a standardized and environment friendly option to characterize and change info.

Understanding XML Construction

1. Root Ingredient: Each XML doc has a single root component that incorporates all different parts. The foundation component is the top-level father or mother of all different parts within the doc.

2. Components and Attributes: XML parts are containers for knowledge and encompass a begin tag, content material, and an finish tag. Attributes present further details about a component and are specified inside the begin tag.

3. Hierarchy and Nesting: XML parts might be nested inside one another, making a hierarchical construction. Every component can include a number of youngster parts, and every youngster component can additional include its personal youngster parts.

Ingredient Construction: An XML component consists of the next parts:

– Begin Tag: The beginning tag signifies the start of a component and contains the component title and any attributes.

– Content material: The content material of a component might be textual content knowledge, different parts (youngster parts), or a mix of each.

– Finish Tag: The tip tag signifies the tip of a component and has the identical title as the beginning tag, besides it’s prefixed with a ahead slash (`

Utilizing Programming Languages to Parse XML

XML parsing entails studying and decoding the construction and knowledge of an XML file utilizing programming languages. Varied programming languages present libraries or APIs for XML parsing, enabling builders to extract and manipulate info from XML paperwork. Listed below are some widespread programming languages and their corresponding XML parsing capabilities:

Java

Java bietet mehrere Möglichkeiten zum Parsen von XML-Dateien:

  1. DOM (Doc Object Mannequin): DOM stellt eine Baumstruktur dar, die das XML-Dokument abbildet. Sie erlaubt den Zugriff auf Knoten, Attribute und Textinhalte im Dokument.
  2. SAX (Easy API for XML): SAX ist ein eventbasierter Parser, der XML-Dokumente sequentiell verarbeitet und Ereignisse auslöst, wenn bestimmte Elemente angetroffen werden.
  3. StAX (Streaming API for XML): StAX ist ein Pull-Parser, der XML-Dokumente in einem Streaming-Verfahren verarbeitet, wodurch eine effizientere Verarbeitung großer XML-Dateien ermöglicht wird.

Jede dieser Java-Bibliotheken bietet unterschiedliche Vorteile je nach den spezifischen Anforderungen der Anwendung.

Python

Python bietet ebenfalls mehrere Bibliotheken für das XML-Parsing:

  1. ElementTree: ElementTree ist eine einfache und leichtgewichtige Bibliothek, die eine Baumstruktur zur Darstellung von XML-Dokumenten verwendet.
  2. lxml: lxml ist eine umfangreiche XML-Parsing-Bibliothek, die sowohl DOM- als auch SAX-Schnittstellen unterstützt und zusätzliche Funktionen wie XPath und XSLT bietet.
  3. xml.etree.ElementTree: Dies ist die Customary-XML-Parsing-Bibliothek in Python und bietet eine einfach zu verwendende Schnittstelle zum Parsen und Bearbeiten von XML-Dokumenten.

Die Wahl der Python-Bibliothek hängt von den Anforderungen der Anwendung und den bevorzugten Funktionen ab.

C#

C# bietet die folgenden Bibliotheken zum Parsen von XML:

  1. System.Xml: System.Xml ist eine umfangreiche Bibliothek, die Unterstützung für DOM, SAX und XPath bietet.
  2. LINQ to XML: LINQ to XML ist eine Sprachintegrierte Abfragesprache, die das Abfragen und Bearbeiten von XML-Dokumenten mit LINQ-Ausdrücken ermöglicht.
  3. XmlSerializer: XmlSerializer ist eine Bibliothek, die XML-Dokumente in .NET-Objekte serialisiert und deserialisiert.

Je nach den spezifischen Anforderungen der Anwendung können Entwickler die am besten geeignete C#-Bibliothek für das XML-Parsing auswählen.

Parsing XML in Python

SAX (Easy API for XML) Parsing

SAX is an event-based XML parser that gives an easy-to-use API to deal with XML occasions. It permits you to course of XML paperwork incrementally, which is very helpful when you might want to course of giant XML recordsdata effectively. SAX supplies the next core strategies which might be known as when particular XML occasions happen:

  • start_element(title, attrs): Referred to as when an XML component begins.
  • end_element(title): Referred to as when an XML component ends.
  • char_data(knowledge): Referred to as when character knowledge is encountered.

Here is an instance of utilizing SAX to parse an XML doc:

“`python
import xml.sax

class MySAXHandler(xml.sax.ContentHandler):
def start_element(self, title, attrs):
print(“Begin component:”, title)

def end_element(self, title):
print(“Finish component:”, title)

def char_data(self, knowledge):
print(“Character knowledge:”, knowledge)

parser = xml.sax.make_parser()
parser.setContentHandler(MySAXHandler())
parser.parse(“instance.xml”)
“`

DOM (Doc Object Mannequin) Parsing

DOM is a tree-based XML parser that gives an object-oriented illustration of an XML doc. It permits you to navigate and manipulate XML paperwork in a hierarchical method. DOM is usually used when you might want to carry out extra complicated operations on XML paperwork, resembling modifying the doc construction or querying the info.

Here is an instance of utilizing DOM to parse an XML doc:

“`python
import xml.dom.minidom

doc = xml.dom.minidom.parse(“instance.xml”)
root = doc.documentElement
print(root.nodeName)
for youngster in root.childNodes:
print(youngster.nodeName, youngster.nodeValue)
“`

lxml Parsing

lxml is a strong and environment friendly XML parser library that gives a wealthy set of options and utilities for working with XML paperwork. It’s constructed on high of libxml2 and libxslt, and it’s notably well-suited for big and sophisticated XML paperwork. lxml supplies various built-in instruments and strategies for parsing, validating, reworking, and manipulating XML paperwork.

Here is an instance of utilizing lxml to parse an XML doc:

“`python
import lxml.etree

root = lxml.etree.parse(“instance.xml”).getroot()
for youngster in root:
print(youngster.tag, youngster.textual content)
“`

Parsing XML in Java

XML (Extensible Markup Language) is extensively used for knowledge illustration in numerous functions. Studying and parsing XML recordsdata in Java is a standard activity for any Java developer. There are a number of methods to parse XML in Java, however one of the vital frequent and highly effective approaches is utilizing the Doc Object Mannequin (DOM) API.

Utilizing the DOM API

The DOM API supplies a hierarchical illustration of an XML doc, permitting builders to navigate and entry its parts and attributes programmatically. Here is find out how to use the DOM API to parse an XML file in Java:

  1. Create a DocumentBuilderFactory object.
  2. Create a DocumentBuilder object utilizing the manufacturing facility.
  3. Parse the XML file utilizing the DocumentBuilder to acquire a Doc object.
  4. Navigate the DOM tree utilizing strategies resembling getElementsByTagName() and getAttribute().

Here is an instance code snippet that demonstrates DOM parsing:


import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Doc;
import org.w3c.dom.NodeList;

public class XMLParserExample {
public static void most important(String[] args) {
strive {
// Create a DocumentBuilderFactory object
DocumentBuilderFactory manufacturing facility = DocumentBuilderFactory.newInstance();

// Create a DocumentBuilder object
DocumentBuilder builder = manufacturing facility.newDocumentBuilder();

// Parse the XML file
Doc doc = builder.parse("instance.xml");

// Get the foundation component
Ingredient rootElement = doc.getDocumentElement();

// Get all youngster parts of the foundation component
NodeList childElements = rootElement.getChildNodes();

// Iterate over the kid parts and print their names
for (int i = 0; i < childElements.getLength(); i++) {
Node youngster = childElements.merchandise(i);
if (youngster.getNodeType() == Node.ELEMENT_NODE) {
System.out.println(youngster.getNodeName());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

On this instance, the DocumentBuilderFactory and DocumentBuilder lessons are used to create a DOM illustration of the XML file. The foundation component is then obtained, and its youngster parts are iterated over and printed. This strategy permits for versatile and in-depth manipulation of the XML doc.

Desk 1: XML Parsing Approaches

| Strategy | Benefits | Disadvantages |
|—|—|—|
| DOM | Hierarchical illustration, versatile navigation | Reminiscence-intensive, slower parsing |
| SAX | Occasion-based, memory-efficient | Restricted navigation capabilities |
| JAXP | API for XML parsing, helps DOM and SAX | Will be complicated to make use of |
| XMLStreamReader | Stream-based parsing, helps partial parsing | Not appropriate for big XML paperwork |

Parsing XML in C#

XML parsing is the method of studying and decoding XML knowledge right into a format that may be processed by a program. In C#, there are a number of methods to parse XML, together with:

1. XMLReader

The XMLReader class supplies a quick and light-weight option to parse XML knowledge. It permits you to learn XML knowledge sequentially, one node at a time.

2. XmlDocument

The XmlDocument class represents an in-memory illustration of an XML doc. It permits you to entry and modify the XML knowledge utilizing a hierarchical construction.

3. XElement

The XElement class represents a component in an XML doc. It supplies a easy and environment friendly option to work with XML knowledge, particularly when you might want to create or modify XML paperwork.

4. XmlSerializer

The XmlSerializer class permits you to serialize and deserialize XML knowledge to and from objects. It’s helpful when you might want to change knowledge between completely different functions or techniques.

5. LINQ to XML

LINQ to XML is a set of extension strategies that permits you to question and manipulate XML knowledge utilizing LINQ (Language Built-in Question). It supplies a handy option to work with XML knowledge in a declarative method.

Navigating XML Information with LINQ to XML

LINQ to XML supplies various strategies for navigating XML knowledge. These strategies can help you choose nodes, filter nodes, and carry out different operations on the XML knowledge. The next desk lists a few of the most typical navigation strategies:

Part Instance
Begin Tag ``

Content material `John Smith`
Finish Tag
Technique Description
Descendants Returns all of the descendant parts of the present component.
Components Returns all of the youngster parts of the present component.
Attributes Returns all of the attributes of the present component.
First Returns the primary matching component within the sequence.
Final Returns the final matching component within the sequence.
Single Returns the one matching component within the sequence.
The place Filters the sequence based mostly on a predicate.

Leveraging XML Parsers and Libraries

Native XML Help in Programming Languages

Many programming languages, resembling Python, Java, and C#, present native XML parsing capabilities. These built-in options supply a handy and standardized option to work together with XML knowledge, simplifying the event course of.

Third-Social gathering XML Parsers and Libraries

For extra complicated or specialised parsing necessities, third-party XML parsers and libraries can present further performance. Some widespread choices embody:

Parser/Library Options
lxml Complete and high-performance XML processing library for Python
xmltodict Converts XML knowledge into Python dictionaries for simple manipulation
Stunning Soup HTML and XML parsing library designed for ease of use and suppleness

Selecting the Proper Choice

The selection of XML parser or library is dependent upon elements resembling language assist, efficiency necessities, and ease of integration. For easy duties, native XML assist could also be ample. For extra complicated or specialised necessities, third-party libraries supply a wider vary of options and capabilities.

DOM (Doc Object Mannequin)

The DOM (Doc Object Mannequin) is a tree-like illustration of an XML doc. It permits builders to navigate and manipulate XML knowledge programmatically, accessing parts, attributes, and textual content nodes.

SAX (Easy API for XML)

SAX (Easy API for XML) is an event-driven XML parsing API. It supplies a easy and environment friendly option to course of XML paperwork sequentially, dealing with occasions resembling the beginning and finish of parts and the prevalence of textual content knowledge.

XPath (XML Path Language)

XPath (XML Path Language) is a question language particularly designed for XML paperwork. It permits builders to navigate and retrieve particular knowledge inside an XML doc based mostly on its construction and content material.

Finest Practices for XML Parsing

1. Use a SAX Parser for Giant XML Recordsdata

SAX parsers are event-driven and do not load the whole XML file into reminiscence. That is extra environment friendly for big XML recordsdata, because it reduces reminiscence utilization and parsing time.

2. Use a DOM Parser for Small XML Recordsdata

DOM parsers load the whole XML file into reminiscence and create a tree-like illustration of the doc. That is extra appropriate for small XML recordsdata, because it permits for quicker random entry to particular parts.

3. Validate Your XML Recordsdata

XML validation ensures that your XML paperwork conform to a predefined schema. This helps to catch errors and inconsistencies early on, enhancing the reliability and interoperability of your XML knowledge.

4. Use Namespaces to Keep away from Ingredient Identify Collisions

Namespaces can help you use the identical component names from completely different XML schemas inside the identical doc. That is helpful for combining knowledge from a number of sources or integrating with exterior functions.

5. Leverage Libraries to Simplify Parsing

XML parsing libraries present helper features and lessons to make it simpler to learn and manipulate XML knowledge. These libraries present a constant interface for several types of XML parsers and supply further options resembling XPath assist.

6. Use XPath to Extract Particular Information

XPath is a language for querying XML paperwork. It permits you to extract particular knowledge parts or nodes based mostly on their location or attributes. XPath expressions can be utilized with each SAX and DOM parsers.

7. Optimize Efficiency by Caching XML Information

Caching XML knowledge can considerably enhance efficiency, particularly if the identical XML recordsdata are accessed a number of instances. Caching might be carried out utilizing in-memory caches or persistent storage options like databases or distributed caching techniques.

Studying XML Recordsdata

XML (Extensible Markup Language) recordsdata are extensively used for knowledge change and storage. To successfully course of and manipulate XML knowledge, it is essential to grasp find out how to learn these recordsdata.

Frequent Challenges and Options

1. Coping with Giant XML Recordsdata

Giant XML recordsdata might be difficult to deal with because of reminiscence constraints. Resolution: Use streaming strategies to course of the file incrementally, with out storing the whole file in reminiscence.

2. Dealing with Invalid XML

XML recordsdata might include invalid knowledge or construction. Resolution: Implement sturdy error dealing with mechanisms to gracefully deal with invalid XML and supply significant error messages.

3. Parsing XML with A number of Roots

XML recordsdata can have a number of root parts. Resolution: Use applicable XML parsing libraries that assist a number of roots, resembling lxml in Python.

4. Dealing with XML Namespace Points

XML parts can belong to completely different namespaces. Resolution: Use namespace mapping to resolve conflicts and facilitate component entry.

5. Parsing XML Paperwork with DTDs

XML paperwork might declare Doc Kind Definitions (DTDs) to validate their construction. Resolution: Use XML validators that assist DTD validation, resembling xmlsec in Python.

6. Processing XML with Schemas

XML paperwork could also be validated in opposition to XML Schemas (XSDs). Resolution: Use XML Schema parsers to make sure adherence to the schema and keep knowledge integrity.

7. Dealing with XML with Unicode Characters

XML recordsdata might include Unicode characters. Resolution: Be certain that your XML parsing library helps Unicode encoding to correctly deal with these characters.

8. Effectively Studying Giant XML Recordsdata utilizing SAX

The Easy API for XML (SAX) is a extensively used event-driven strategy for parsing giant XML recordsdata. Resolution: Make the most of SAX’s streaming capabilities to keep away from reminiscence bottlenecks and obtain environment friendly parsing even for large XML recordsdata.

SAX Occasion Triggered
startElement Begin of a component
characters Character knowledge inside a component
endElement Finish of a component

Dealing with Exceptions and Error Instances

9. Dealing with Completely different Errors

There are a number of sources of errors when studying XML recordsdata, resembling syntax errors, I/O errors, and validation errors. Every sort of error requires a particular dealing with technique.

Syntax errors happen when the XML file doesn’t conform to the XML syntax guidelines. These errors are detected throughout parsing and might be dealt with by catching the XMLSyntaxError exception.

I/O errors happen when there are issues studying the XML file from the enter supply. These errors might be dealt with by catching the IOError exception.

Validation errors happen when the XML file doesn’t conform to the required schema. These errors might be dealt with by catching the XMLValidationError exception.

To deal with all varieties of errors, use a try-except block that catches all three exceptions.

Error Varieties and Dealing with Exceptions
Error Kind Exception
Syntax Error XMLSyntaxError
I/O Error IOError
Validation Error XMLValidationError

Superior XML Parsing Methods

For extra complicated XML parsing wants, think about using the next superior strategies:

1. Utilizing Common Expressions

Common expressions can be utilized to match patterns inside XML paperwork. This may be helpful for extracting particular knowledge or validating XML construction. For instance, the next common expression can be utilized to match all parts with the title “buyer”:

<buyer.*?>

2. Utilizing XSLT

XSLT (Extensible Stylesheet Language Transformations) is a language used to remodel XML paperwork into different codecs. This may be helpful for changing XML knowledge into HTML, textual content, or different codecs. For instance, the next XSLT can be utilized to transform an XML doc into an HTML desk:

<xsl:stylesheet model="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Remodel">
  <xsl:template match="/">
    <desk>
      <xsl:for-each choose="//buyer">
        <tr>
          <td><xsl:value-of choose="title"/></td>
          <td><xsl:value-of choose="deal with"/></td>
        </tr>
      </xsl:for-each>
    </desk>
</xsl:stylesheet>

3. Utilizing XPath

XPath (XML Path Language) is a language used to navigate and choose nodes inside XML paperwork. This may be helpful for shortly accessing particular knowledge or modifying the construction of an XML doc. For instance, the next XPath expression can be utilized to pick out all parts with the title “buyer”:

/prospects/buyer

4. Utilizing DOM

The DOM (Doc Object Mannequin) is a tree-like illustration of an XML doc. This may be helpful for manipulating the construction of an XML doc or accessing particular knowledge. For instance, the next code can be utilized to get the title of the primary buyer in an XML doc:

const doc = new DOMParser().parseFromString(xml, "textual content/xml");
const customerName = doc.querySelector("buyer").getAttribute("title");

5. Utilizing SAX

SAX (Easy API for XML) is an event-based parser that permits you to course of XML paperwork in a streaming trend. This may be helpful for parsing giant XML paperwork or when you might want to course of the info as it’s being parsed. For instance, the next code can be utilized to print the title of every buyer in an XML doc:

const parser = new SAXParser();
parser.parse(xml, {
  startElement: perform(title, attrs) {
    if (title === "buyer") {
      console.log(attrs.title);
    }
  }
});

6. Utilizing XML Schema

XML Schema is a language used to outline the construction and content material of XML paperwork. This may be helpful for validating XML paperwork and guaranteeing that they conform to a particular schema. For instance, the next XML Schema can be utilized to outline an XML doc that incorporates buyer info:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:component title="prospects">
    <xs:complexType>
      <xs:sequence>
        <xs:component title="buyer" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:component title="title" sort="xs:string"/>
              <xs:component title="deal with" sort="xs:string"/>
            </xs:sequence>
          </xs:complexType>
        </xs:component>
      </xs:sequence>
    </xs:complexType>
  </xs:component>
</xs:schema>

7. Utilizing XML Namespaces

XML Namespaces are used to establish the origin of parts and attributes in an XML doc. This may be helpful for avoiding conflicts between parts and attributes from completely different sources. For instance, the next XML doc makes use of namespaces to distinguish between parts from the “buyer” namespace and the “deal with” namespace:

<prospects xmlns:cust="http://instance.com/prospects" xmlns:addr="http://instance.com/addresses">
  <cust:buyer>
    <cust:title>John Smith</cust:title>
    <addr:deal with>123 Principal Road</addr:deal with>
  </cust:buyer>
</prospects>

8. Utilizing XML Canonicalization

XML Canonicalization is a course of that converts an XML doc right into a canonical kind. This may be helpful for evaluating XML paperwork or creating digital signatures. For instance, the next code can be utilized to canonicalize an XML doc:

const canonicalizer = new XMLSerializer();
const canonicalizedXML = canonicalizer.canonicalize(xml);

9. Utilizing XML Encryption

XML Encryption is a course of that encrypts an XML doc utilizing a specified encryption algorithm. This may be helpful for shielding delicate knowledge in XML paperwork. For instance, the next code can be utilized to encrypt an XML doc utilizing the AES-256 encryption algorithm:

const encryptor = new XMLCryptor(aes256Key);
const encryptedXML = encryptor.encrypt(xml);

10. Utilizing XML Digital Signatures

XML Digital Signatures are used to confirm the authenticity and integrity of an XML doc. This may be helpful for guaranteeing that an XML doc has not been tampered with. For instance, the next code can be utilized to create a digital signature for an XML doc:

const signer = new XMLSigner(privateKey);
const signature = signer.signal(xml);

Easy methods to Learn XML Recordsdata

XML (Extensible Markup Language) is a extensively used markup language for storing and transmitting knowledge. It’s a versatile and extensible format that can be utilized to characterize all kinds of information constructions. Studying XML recordsdata is a standard activity in lots of programming languages.

Python

In Python, the xml module supplies a easy and handy option to learn XML recordsdata. The next code exhibits find out how to learn an XML file and entry its parts:

import xml.etree.ElementTree as ET

tree = ET.parse('instance.xml')
root = tree.getroot()

for youngster in root:
    print(youngster.tag, youngster.textual content)

Java

In Java, the javax.xml.parsers package deal supplies various lessons for parsing XML recordsdata. The next code exhibits find out how to learn an XML file and entry its parts:

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Doc;
import org.w3c.dom.NodeList;

DocumentBuilderFactory manufacturing facility = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = manufacturing facility.newDocumentBuilder();
Doc doc = builder.parse("instance.xml");

NodeList nodes = doc.getElementsByTagName("tag");
for (int i = 0; i < nodes.getLength(); i++) {
    System.out.println(nodes.merchandise(i).getTextContent());
}

Individuals Additionally Ask

How do I learn an XML file from a URL?

In Python, you should use the requests library to learn an XML file from a URL:

import requests
from xml.etree.ElementTree import fromstring

response = requests.get('https://instance.com/instance.xml')
tree = fromstring(response.content material)

In Java, you should use the java.internet.URL class to learn an XML file from a URL:

import java.internet.URL;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Doc;

URL url = new URL("https://instance.com/instance.xml");
DocumentBuilderFactory manufacturing facility = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = manufacturing facility.newDocumentBuilder();
Doc doc = builder.parse(url.openStream());

How do I parse an XML file with attributes?

In Python, you possibly can entry the attributes of an XML component utilizing the attrib dictionary:

for youngster in root:
    print(youngster.tag, youngster.textual content, youngster.attrib)

In Java, you possibly can entry the attributes of an XML component utilizing the getAttributes() methodology:

NodeList nodes = doc.getElementsByTagName("tag");
for (int i = 0; i < nodes.getLength(); i++) {
    NamedNodeMap attributes = nodes.merchandise(i).getAttributes();
    for (int j = 0; j < attributes.getLength(); j++) {
        System.out.println(attributes.merchandise(j).getName() + ": " + attributes.merchandise(j).getValue());
    }
}

How do I write an XML file?

In Python, you should use the xml.etree.ElementTree module to jot down XML recordsdata:

import xml.etree.ElementTree as ET

root = ET.Ingredient("root")
youngster = ET.SubElement(root, "youngster")
youngster.textual content = "textual content"

tree = ET.ElementTree(root)
tree.write("instance.xml")

In Java, you should use the javax.xml.remodel package deal to jot down XML recordsdata:

import javax.xml.remodel.Transformer;
import javax.xml.remodel.TransformerFactory;
import javax.xml.remodel.dom.DOMSource;
import javax.xml.remodel.stream.StreamResult;

TransformerFactory manufacturing facility = TransformerFactory.newInstance();
Transformer transformer = manufacturing facility.newTransformer();
DOMSource supply = new DOMSource(doc);
StreamResult consequence = new StreamResult(new File("instance.xml"));
transformer.remodel(supply, consequence);