includes in xml-files?

M

Magnus Warker

Hi,

I have an XML-File in the classpath and I read it in with
getResourceAsStream (Pseudocode):

InputStream stm = class.getResourceAsStream (...);
DocumentBuilderFactory ftr = DocumentBuilderFactory.newInstance ();
ftr.setIgnoringComments (true);
DocumentBuilder bld = ftr.newDocumentBuilder();
Document doc = bld.parse (stm);

Now I would like to source out some of the contents into separate
xml-files and include them into the main file.

Is this possible, in particular when the files are loaded as resource
stream?

How can I do that? WhatÄs the syntax for the include directives?

Thanks
Magnus
 
R

Roedy Green

How can I do that? WhatÄs the syntax for the include directives?

I have nested includes in the HTMLMacros processor.

You need an Include object that keeps track of the mother file and
where you were in it, or simply do it with recursive calls with that
info tracked in local variables.

What syntax? I have never heard of anything part of the XML protocol.
If you want to be able to include in the middle of a field you will
need something that won't be confused with data.
You could do it with something that looked a bit like real XML

e.g.

<include "file.txt" /> (which can even appear in the middle of a field
or between XML tags. The embedded text need not contain balanced tags.

You might want to decorate it a bit to avoid conflicting with real xml
e.g.
<@include "file.txt" />

You might want to make it look more like real XML, e.g.

<include file="file.txt />
or
<@include>file.txt</@include>

You might to make it look like perfectly legit XML so the XML parser
will find and parse the commands for you. Otherwise you will have to
look ahead yourself or preprocess the file to create the complet file
before feeding it to the XML processor.





--
Roedy Green Canadian Mind Products
http://mindprod.com
Capitalism has spurred the competition that makes CPUs faster and
faster each year, but the focus on money makes software manufacturers
do some peculiar things like deliberately leaving bugs and deficiencies
in the software so they can soak the customers for upgrades later.
Whether software is easy to use, or never loses data, when the company
has a near monopoly, is almost irrelevant to profits, and therefore
ignored. The manufacturer focuses on cheap gimicks like dancing paper
clips to dazzle naive first-time buyers. The needs of existing
experienced users are almost irrelevant. I see software rental as the
best remedy.
 
A

Arved Sandstrom

Hi,

I have an XML-File in the classpath and I read it in with
getResourceAsStream (Pseudocode):

InputStream stm = class.getResourceAsStream (...);
DocumentBuilderFactory ftr = DocumentBuilderFactory.newInstance ();
ftr.setIgnoringComments (true);
DocumentBuilder bld = ftr.newDocumentBuilder();
Document doc = bld.parse (stm);

Now I would like to source out some of the contents into separate
xml-files and include them into the main file.

Is this possible, in particular when the files are loaded as resource
stream?

How can I do that? WhatÄs the syntax for the include directives?

Thanks
Magnus

One standard way of including XML is external parsed entities. See
http://www.w3.org/TR/2004/REC-xml-20040204/#sec-external-ent.

If you've done that then there is actually no other coding that you need
to do. Simply continue to reference the main XML that includes other
XML; the parser is responsible for locating and parsing the includes.

AHS

--
You should know the problem before you try to solve it.
Example: When my son was three he cried about a problem with his hand. I
kissed it several times and asked him about the problem. He peed on his
hand.
-- Radia Perlman, inventor of spanning tree protocol
 
A

Arne Vajhøj

I have an XML-File in the classpath and I read it in with
getResourceAsStream (Pseudocode):

InputStream stm = class.getResourceAsStream (...);
DocumentBuilderFactory ftr = DocumentBuilderFactory.newInstance ();
ftr.setIgnoringComments (true);
DocumentBuilder bld = ftr.newDocumentBuilder();
Document doc = bld.parse (stm);

Now I would like to source out some of the contents into separate
xml-files and include them into the main file.

Is this possible, in particular when the files are loaded as resource
stream?

How can I do that? WhatÄs the syntax for the include directives?

XML is more a family of languages than it is a language.

So I would expect includes to be done using an element
specific for the particular XML dialect.

This also means that the XML parser itself can not do
the processing, but the application will need to handle
that.

Arne
 
J

Jeff Higgins

Hi,

I have an XML-File in the classpath and I read it in with
getResourceAsStream (Pseudocode):

InputStream stm = class.getResourceAsStream (...);
DocumentBuilderFactory ftr = DocumentBuilderFactory.newInstance ();
ftr.setIgnoringComments (true);
DocumentBuilder bld = ftr.newDocumentBuilder();
Document doc = bld.parse (stm);

Now I would like to source out some of the contents into separate
xml-files and include them into the main file.

Is this possible, in particular when the files are loaded as resource
stream?

How can I do that? WhatÄs the syntax for the include directives?
XInclude? <http://www.w3.org/TR/xinclude/>
I haven't used it, so I don't know how well it's supported.
I think JAXP has some support.
 
R

Roedy Green

One standard way of including XML is external parsed entities. See
http://www.w3.org/TR/2004/REC-xml-20040204/#sec-external-ent.

I am amazed you deduced that lawyerly document anything to with
including text in XML files.
--
Roedy Green Canadian Mind Products
http://mindprod.com
Capitalism has spurred the competition that makes CPUs faster and
faster each year, but the focus on money makes software manufacturers
do some peculiar things like deliberately leaving bugs and deficiencies
in the software so they can soak the customers for upgrades later.
Whether software is easy to use, or never loses data, when the company
has a near monopoly, is almost irrelevant to profits, and therefore
ignored. The manufacturer focuses on cheap gimicks like dancing paper
clips to dazzle naive first-time buyers. The needs of existing
experienced users are almost irrelevant. I see software rental as the
best remedy.
 
R

Roedy Green

Hi,

I have an XML-File in the classpath and I read it in with
getResourceAsStream (Pseudocode):

InputStream stm = class.getResourceAsStream (...);
DocumentBuilderFactory ftr = DocumentBuilderFactory.newInstance ();
ftr.setIgnoringComments (true);
DocumentBuilder bld = ftr.newDocumentBuilder();
Document doc = bld.parse (stm);

Now I would like to source out some of the contents into separate
xml-files and include them into the main file.

Is this possible, in particular when the files are loaded as resource
stream?

How can I do that? WhatÄs the syntax for the include directives?

Here are a couple of kludges. At least they are comprehensible.
http://www.dpawson.co.uk/xsl/sect2/N4760.html#d6065e238
--
Roedy Green Canadian Mind Products
http://mindprod.com
Capitalism has spurred the competition that makes CPUs faster and
faster each year, but the focus on money makes software manufacturers
do some peculiar things like deliberately leaving bugs and deficiencies
in the software so they can soak the customers for upgrades later.
Whether software is easy to use, or never loses data, when the company
has a near monopoly, is almost irrelevant to profits, and therefore
ignored. The manufacturer focuses on cheap gimicks like dancing paper
clips to dazzle naive first-time buyers. The needs of existing
experienced users are almost irrelevant. I see software rental as the
best remedy.
 
A

Arne Vajhøj

XML is more a family of languages than it is a language.

So I would expect includes to be done using an element
specific for the particular XML dialect.

This also means that the XML parser itself can not do
the processing, but the application will need to handle
that.

XML schemas use:

http://www.w3schools.com/schema/el_include.asp

JSP pages use:

http://java.sun.com/products/jsp/tags/12/syntaxref1214.html

Facelets use:

http://www.jsftoolbox.com/documentation/facelets/10-TagReference/facelets-ui-include.html

etc..

Arne
 
A

Arved Sandstrom

I am amazed you deduced that lawyerly document anything to with
including text in XML files.

Well, truth be told, I thought it was pretty obvious. :) The link you
supplied to that one section of Dave Pawson's site, if you left out the
XSLT stuff, bears that out (Jeni Tennison's answer that you referenced
in particular). It's not so much that I had to investigate the XML spec
to find this, I already knew about it - I thought it was commonplace
knowledge. The entity approach has the advantage of being part of the
XML spec.

Now Magnus mentioned external XML files so that's why I referenced
external parsed entities specifically.

I won't disparage XInclude. It has advantages, notably not needing to
pre-declare external entities in the DTD (which may or may not be an
issue for Magnus). Section 1.2 in http://www.w3.org/TR/xinclude/ should
be read when deciding between the two approaches.

On the subject of XInclude, if the OP wished to try that approach to use
something like

<xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
href="inclusion.xml" parse="xml" />

(the parse="xml" is the default actually) then he can modify his
original code with

ftr.setXIncludeAware(true);
ftr.setNamespaceAware(true);

and go to town.

AHS
--
You should know the problem before you try to solve it.
Example: When my son was three he cried about a problem with his hand. I
kissed it several times and asked him about the problem. He peed on his
hand.
-- Radia Perlman, inventor of spanning tree protocol
 
A

Arved Sandstrom

XInclude? <http://www.w3.org/TR/xinclude/>
I haven't used it, so I don't know how well it's supported.
I think JAXP has some support.
Certainly fine out of the box in 1.6, probably since 1.5. See my other
post for turning on XInclude.

AHS
--
You should know the problem before you try to solve it.
Example: When my son was three he cried about a problem with his hand. I
kissed it several times and asked him about the problem. He peed on his
hand.
-- Radia Perlman, inventor of spanning tree protocol
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top