© Philippe PRADOS

Lecture d'un fichier XML

<?xml version="1.0" encoding="UTF-8" ?>
<configurations>
<item name="user">me</item>
<item name="password">sesame</item>
</
configurations>


public class MonOutil
{
public static final DocumentBuilderFactory factory;
static
{
factory=DocumentBuilderFactory.newInstance();
}

public static void main(String[] args)
throws SAXException, IOException, ParserConfigurationException
{
final File filename=new File(args[0]);

// Get document
final Document jndiparams = factory.newDocumentBuilder().parse(filename);

// Use XML data
System.out.println("Process "+filename);

// Get head
final Node head=jndiparams.getFirstChild();

final NodeList childs=head.getChildNodes();
for (int i=0;i<childs.getLength();++i)
{
Node node=childs.item(i);
if (node.getNodeType()!=Node.ELEMENT_NODE) continue;
System.
out.println(node.getAttributes()
.getNamedItem(
"name").getNodeValue()+"="
+node.getTextContent());
}
System.
out.println();
}
}


public class MonOutil
{

public static final DocumentBuilderFactory factory;
static
{
factory=DocumentBuilderFactory.newInstance();
}

public static void main(String[] args)
throws SAXException, IOException, ParserConfigurationException
{

// Get filename
final File filename=new File(args[0]);
final URL filename = nameToURL(args[0]);

// Get document
final Document jndiparams = factory.newDocumentBuilder().parse(filename);
final InputStream in=filename.openStream();
final Document jndiparams;
try
{
jndiparams =
factory.newDocumentBuilder().parse(in);
}
finally
{
in.close();
}


// Use XML data

...
}
private static URL nameToURL(String name) throws MalformedURLException
{
try
{
return new URL(name);
}
catch (MalformedURLException x)
{
return new File(name).toURI().toURL();
}
}
}


<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.prados.eu/xsd/MonOutil/1.0/"
xmlns:tns="http://www.prados.eu/xsd/MonOutil/1.0/"
elementFormDefault="qualified" attributeFormDefault="unqualified">

<element name="configurations">
<complexType>
<sequence minOccurs="1" maxOccurs="unbounded">
<element ref="tns:item" />
</sequence>
<anyAttribute processContents="strict" />
</complexType>
</element>
<element name="item">
<complexType>
<simpleContent>
<extension base="string">
<attribute name="name" type="string" />
</extension>
</simpleContent>
</complexType>
</element>
</
schema>


<?xml version="1.0" encoding="UTF-8" ?>
<configurations
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.prados.eu/xsd/MonOutil/1.0/
MonOutil.xsd"
xmlns="http://www.prados.eu/xsd/MonOutil/1.0/"
>
<item name="user">me</item>
<item name="password">sesame</item>
</
configurations>


public class MonOutil
{

public static final DocumentBuilderFactory factory;
static
{
factory=DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
factory.setAttribute(
"http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
}

public static void main(String[] args)
throws SAXException, IOException, ParserConfigurationException
{
// Get filename
final URL filename = nameToURL(args[0]);

// Get document
final InputStream in=filename.openStream();
final Document jndiparams;
try
{
jndiparams =
factory.newDocumentBuilder().parse(in);
}
finally
{
in.close();
}

// Use XML data
System.out.println("Process "+filename);

// Get head
final Node head=jndiparams.getFirstChild();

final NodeList childs=head.getChildNodes();
for (int i=0;i<childs.getLength();++i)
{
Node node=childs.item(i);
if (node.getNodeType()!=Node.ELEMENT_NODE) continue;
if (node.getNamespaceURI()!=
"http://www.prados.eu/xsd/MonOutil/1.0/") continue;
System.out.println(node.getAttributes()
.getNamedItem(
"name").getNodeValue()+"="
+node.getTextContent());

}
System.
out.println();
}

...

}


<?xml version="1.0" encoding="UTF-8" ?>
<configurations
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.prados.eu/xsd/MonOutil/1.0/
http://www.prados.eu/xsd/1.0/MonOutil.xsd"
xmlns="http://www.prados.eu/xsd/MonOutil/1.0/"
>
<item name="user">me</item>
<item name="password">sesame</item>
</
configurations>


<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.prados.eu/xsd/MonOutil/1.0/"
xmlns:tns="http://www.prados.eu/xsd/MonOutil/1.0/"
elementFormDefault="qualified" attributeFormDefault="unqualified">

<element name="configurations">
<complexType>
<sequence minOccurs="1" maxOccurs="unbounded">
<choice minOccurs="1" maxOccurs="unbounded">
<element ref="tns:item" />
<any processContents="lax" namespace="##other" />
</choice>
</sequence>
<attribute name="id" type="ID" use="optional"/>
<anyAttribute processContents="lax" namespace="##other" />
</complexType>
</element>
<element name="item">
<complexType>
<simpleContent>
<extension base="string">
<attribute name="id" type="ID" use="optional"/>
<attribute name="name" type="string" />
<anyAttribute processContents="lax" namespace="##other"/>
</extension>
</simpleContent>
</complexType>
</element>
</
schema>


<?xml version="1.0" encoding="UTF-8" ?>
<configurations
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.prados.eu/xsd/MonOutil/1.0/
http://www.prados.eu/xsd/1.0/MonOutil.xsd"
xmlns:ext="http://www.integrateur.fr/xsd/MesAjouts/1.0/"
xmlns="http://www.prados.eu/xsd/MonOutil/1.0/"
id="root"
>
<ext:comment xml:lang="fr">Utilisateur par défaut</ext:comment>
<item name="user" xml:lang="fr" id="nom">me</item>
<item name="password">sesame</item>
</
configurations>


<?xml version="1.0" encoding="UTF-8"?>
<
nodes
xmlns="http://www.integrator.org"
xmlns:conf="http://www.prados.eu/xsd/MonOutil/1.0/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.integrator.org Nodes.xsd">
<node name="jmshost">
<conf:configurations id="jms">
<conf:item name="user">me</conf:item>
<conf:item name="password">sesame</conf:item>
</conf:configurations>
</node>
<node name="dbhost">
<conf:configurations id="db">
<conf:item name="user">dba</conf:item>
<conf:item name="password">sesame</conf:item>
</conf:configurations>
</node>
</
nodes>


<?xml version="1.0" encoding="UTF-8"?>
<schema
targetNamespace="http://www.integrator.org"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:conf="http://www.prados.eu/xsd/MonOutil/1.0/"
>
<import namespace="http://www.prados.eu/xsd/MonOutil/1.0/"
schemaLocation="http://www.prados.eu/xsd/1.0/MonOutil.xsd"/>
<element name="nodes">
<complexType>
<sequence maxOccurs="unbounded">
<element name="node">
<complexType>
<sequence>
<element ref="conf:configurations"/>
</sequence>
<attribute name="name" type="ID" use="required"/>
</complexType>
</element>
</sequence>
</complexType>
</element>
</
schema>


public class MonOutil
{
public static final DocumentBuilderFactory factory;

static
{
factory=DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
factory.setAttribute(
"http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
}
private static final String namespace="http://www.prados.eu/xsd/MonOutil/1.0/";

public static void main(String[] args)
throws SAXException, IOException, ParserConfigurationException, URISyntaxException
{
// Get filename
final URL filename = nameToURL(args[0]);

// Get document
...


// Use XML data

System.out.println("Process "+filename);

// Get head
final Node head=jndiparams.getFirstChild();
final Node head=(null==filename.getRef())
? jndiparams.getFirstChild()
: jndiparams.getElementById(filename.getRef());

final NodeList childs=head.getChildNodes();
for (int i=0;i<childs.getLength();++i)
{
Node node=childs.item(i);
if (node.getNodeType()!=Node.ELEMENT_NODE) continue;
if (node.getNamespaceURI()!=namespace) continue;
System.
out.println(node.getAttributes().getNamedItem("name").getNodeValue()+"="
+node.getTextContent());
}
System.
out.println();
}

private static URL nameToURL(String name) throws MalformedURLException
{
try
{
return new URL(name);
}
catch (MalformedURLException x)
{
final int idx=name.indexOf('#');
if (idx!=-1)
return new URL(
new File(name.substring(0,idx)).toURI()
.toASCIIString()+name.substring(idx));
else
return new File(name).toURI().toURL();
}
}
}



Les propriétés

// Get properties filename
final String propfilename=args[0];
final Properties prop=new Properties();
{
InputStream in=
null;
try
{
in=
new BufferedInputStream(new FileInputStream(propfilename));
prop.load(in);
}
finally
{
if (in!=null) in.close();
}
}


// Get properties filename
final String propfilename=args[0];
URL propfilename;
try
{
propfilename =
new URL(args[0]);
}
catch (MalformedURLException x)
{
propfilename =
new File(args[0]).toURI().toURL();
}

// Load properties
Properties prop=new Properties();
{
InputStream in=
null;
try
{
in=new BufferedInputStream(new FileInputStream(propfilename));
in=new BufferedInputStream(propfilename.openStream());
prop.load(in);
}
finally
{
if (in!=null) in.close();
}
}


// Load properties
Properties prop=new Properties();
{
InputStream in=
null;
try
{
in=
new BufferedInputStream(propfilename.openStream());
if (propfilename.getPath().endsWith(".xml"))
prop.loadFromXML(in);
else
prop.load(in);
}
finally
{
if (in!=null) in.close();
}
}


<?xml version="1.0" encoding="UTF-8" ?>
<!
DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<
properties version="1.0">
<entry key="user">aladin</entry>
<entry key="password">sesame</entry>
</
properties>


// Get document
final InputStream in=filename.openStream();
final Document jndiparams;
try
{
jndiparams = factory.newDocumentBuilder().parse(in);
jndiparams = factory.newDocumentBuilder().parse(
new InputSource(new VariableReader(new InputStreamReader(in),prop)));
}
finally
{
in.close();
}


<?xml version="1.0" encoding="UTF-8"?>
<nodes
xmlns="http://www.integrator.org"
xmlns:conf="http://www.prados.eu/xsd/MonOutil/1.0/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.integrator.org Nodes.xsd">
<node name="jmshost">
<conf:configurations id="jms">
<conf:item name="user">${user}</conf:item>
<conf:item name="password">${password}</conf:item>
</conf:configurations>
</node>
</
nodes>



2003/1.0 pp@philippe.prados.name 7/7