SAXParser xinclude problem

Discussion in 'Java' started by Andi, Dec 1, 2008.

  1. Andi

    Andi Guest

    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
     
    Andi, Dec 1, 2008
    #1
    1. Advertising

  2. Andi

    Patel Guest

    On Dec 1, 5:32 pm, Andi <> wrote:
    > 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.
     
    Patel, Dec 2, 2008
    #2
    1. Advertising

  3. Andi

    Lew Guest

    Patel wrote:
    > Use the following code
    > Document doc = reader.read(new File
    >> (AppTest.class.getResource("/tools.xml").toURI()));

    > doc.getElementById(Temperature);


    This is not SAX.

    --
    Lew
    Set off a sig by "-- " (dash dash space) on a line by itself.
     
    Lew, Dec 2, 2008
    #3
  4. Andi

    Daniel Pitts Guest

    Lew wrote:
    > Patel wrote:
    >> Use the following code
    >> Document doc = reader.read(new File
    >>> (AppTest.class.getResource("/tools.xml").toURI()));

    >> doc.getElementById(Temperature);

    >
    > 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.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Dec 2, 2008
    #4
  5. Andi

    Lew Guest

    On Dec 2, 3:23 pm, Daniel Pitts
    <> wrote:
    > Lew wrote:
    > > Patel wrote:
    > >> Use the following code
    > >> Document doc = reader.read(new File
    > >>> (AppTest.class.getResource("/tools.xml").toURI()));
    > >> doc.getElementById(Temperature);

    >
    > > 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.


    Ah. Thanks for the clarification.

    --
    Lew
     
    Lew, Dec 2, 2008
    #5
  6. Andi

    Pappy :-\) Guest

    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.
     
    Pappy :-\), Dec 3, 2008
    #6
  7. Andi

    Andi Guest

    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
     
    Andi, Dec 3, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SL
    Replies:
    0
    Views:
    525
  2. stacey
    Replies:
    11
    Views:
    634
    Adam Maass
    Mar 6, 2007
  3. Replies:
    0
    Views:
    452
  4. Replies:
    1
    Views:
    310
    Pete Becker
    Dec 20, 2006
  5. Peter Higgins

    libxml's SaxParser and UTF-8 problem

    Peter Higgins, Mar 2, 2007, in forum: Ruby
    Replies:
    2
    Views:
    115
    Jenda Krynicky
    Mar 7, 2007
Loading...

Share This Page