SAXParser xinclude problem

A

Andi

Hi,

I have following xml files:

tools.xml:

<?xml version="1.0" encoding="utf-8"?>
<Tools xmlns:xi="http://www.w3.org/2001/XInclude">
<Tool name="tool1" id="1">
<xi:include href="instruments.xml" xpointer="element(/1/1)"/>
</Tool>
</Tools>

instruments.xml:

<?xml version="1.0" encoding="utf-8"?>
<Instruments>
<Instrument id="Temperature" type="Temperature" name=""/>
<Instrument id="Humidity" type="Humidity" name=""/>
<Instrument id="Massflow" type="Massflow" name=""/>
</Instruments>

I am using the following code to parse the xml:

public void testApp()
{
try
{
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setXIncludeAware(true);
factory.setNamespaceAware(false);
factory.setValidating(false);

SAXParser parser = factory.newSAXParser();

SAXReader reader = new SAXReader(parser.getXMLReader());
Document doc = reader.read(new File
(AppTest.class.getResource("/tools.xml").toURI()));

XMLWriter writer = new XMLWriter(System.out);
writer.write(doc);
}
catch (Exception e)
{
e.printStackTrace();
}
}

With
<xi:include href="instruments.xml" xpointer="element(/1/1)"/>

it works. Now I want to get the element with the id "Temperature".
Don't know how to do this.

Using jdk-1.6_10 and dom4j-1.6.1

Thanks for every hint
 
P

Patel

Hi,

I have following xml files:

tools.xml:

<?xml version="1.0" encoding="utf-8"?>
<Tools xmlns:xi="http://www.w3.org/2001/XInclude">
        <Tool name="tool1" id="1">
                <xi:include href="instruments.xml" xpointer="element(/1/1)"/>
        </Tool>
</Tools>

instruments.xml:

<?xml version="1.0" encoding="utf-8"?>
<Instruments>
  <Instrument id="Temperature" type="Temperature" name=""/>
  <Instrument id="Humidity" type="Humidity" name=""/>
  <Instrument id="Massflow" type="Massflow" name=""/>
</Instruments>

I am using the following code to parse the xml:

    public void testApp()
    {
        try
        {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setXIncludeAware(true);
            factory.setNamespaceAware(false);
            factory.setValidating(false);

            SAXParser parser = factory.newSAXParser();

            SAXReader reader = new SAXReader(parser.getXMLReader());
            Document doc = reader.read(new File
(AppTest.class.getResource("/tools.xml").toURI()));

            XMLWriter writer = new XMLWriter(System.out);
            writer.write(doc);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

With
                <xi:include href="instruments.xml" xpointer="element(/1/1)"/>

it works. Now I want to get the element with the id "Temperature".
Don't know how to do this.

Using jdk-1.6_10 and dom4j-1.6.1

Thanks for every hint
Use the following code
Document doc = reader.read(new File
(AppTest.class.getResource("/tools.xml").toURI()));
doc.getElementById(Temperature);

Thanks,
Patel.
 
D

Daniel Pitts

Lew said:
This is not SAX.
Nor was the OP's code. The OP is using a SAXParser to build a DOM, and
was confused by how to use the DOM properly after that.
 
L

Lew

Nor was the OP's code. The OP is using a SAXParser to build a DOM, and
was confused by how to use the DOM properly after that.

Ah. Thanks for the clarification.
 
P

Pappy :-\)

You might want to check out some of these online resources:

Processing XML with Java http://www.cafeconleche.org/books/xmljava/

Free Java tutorials & programming source code http://www.freejavaguide.com/

it works. Now I want to get the element with the id "Temperature".
Don't know how to do this.

Using jdk-1.6_10 and dom4j-1.6.1

Thanks for every hint
Use the following code
Document doc = reader.read(new File
(AppTest.class.getResource("/tools.xml").toURI()));
doc.getElementById(Temperature);

Thanks,
Patel.
 
A

Andi

Sorry, but my problem is the xpointer query. Is there a way to change
the xpointer query that it just includes this single node.
Thanks
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top