Using XPath to copy nodes

Discussion in 'Java' started by darkcloudinc@gmail.com, Mar 4, 2009.

  1. Guest

    Hi,

    I'm thinking I'm missing something in the middle. Currently, I'm
    optimizing this one project to dump a table into an XML document
    temporarily to minimize the number of hits on the database. The
    original logic had struck the database at a minimum of 200 per
    creation of each document thus leading to 6 documents a minute which
    is not that fast when I need to generation 50k documents.

    The design so far it:
    ---
    private Element getAnswers(Element root, String queID) throws
    Exception{
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();

    //Design the query to search through the XML Document
    String xpQry = "/answers/ques[@sequence='" + SeqNo + "' and
    @item_id='" + queID + "']/answ";
    //Execute the query
    XPathExpression expr = xpath.compile(xpQry);
    Object result = expr.evaluate(ansDoc, XPathConstants.NODESET);

    //Store the Results
    NodeList nodes = (NodeList) result;

    //Exit if there is no results found
    if(nodes.getLength() == 0){
    return root;
    }

    //Get ID for later use
    xpQry = "/answers/ques[@sequence='" + SeqNo + "' and
    @item_id='" + queID + "']/@id";
    expr = xpath.compile(xpQry);
    result = expr.evaluate(ansDoc, XPathConstants.NODESET);
    NodeList ansID = (NodeList) result;

    //Does not append anything
    for(int nCnt=0;nCnt==nodes.getLength();nCnt++){
    Element answ = (Element) nodes.item(nCnt);

    root.appendChild(answ);
    }

    return root;
    }
    ===

    Sample Data:
    ---
    <?xml version='1.0'?>

    <answers>
    <ques id='9447FCB8-1F77-422A-8FDA-7713CC7AE289' item_id='3BDF6103-
    FC8C-4697-B93F-A49BAAAC97F2' sequence='0'>
    <answ answered='2009-03-02 13:23:00.857' device='system'
    personnel='DTS, DTS'><![CDATA[Hall/Foyer]]></answ></ques></answers>
    ===

    Ideally, I'd like to use XPath to query the structure for the node to
    be appended onto another document. But I don't want to have to resort
    to searching line by line for the node I need as it may not at the
    speed in which I seek. However, I'm open to other suggestions.

    Thanks
    Dominc
     
    , Mar 4, 2009
    #1
    1. Advertising

  2. Guest

    On Mar 4, 10:33 am, wrote:
    > Hi,
    >
    > I'm thinking I'm missing something in the middle.  Currently, I'm
    > optimizing this one project to dump a table into an XML document
    > temporarily to minimize the number of hits on the database.  The
    > original logic had struck the database at a minimum of 200 per
    > creation of each document thus leading to 6 documents a minute which
    > is not that fast when I need to generation 50k documents.
    >
    > The design so far it:
    > ---
    >     private Element getAnswers(Element root, String queID) throws
    > Exception{
    >         XPathFactory factory = XPathFactory.newInstance();
    >         XPath xpath = factory.newXPath();
    >
    > //Design the query to search through the XML Document
    >         String xpQry = "/answers/ques[@sequence='" + SeqNo + "' and
    > @item_id='" + queID + "']/answ";
    > //Execute the query
    >         XPathExpression expr = xpath.compile(xpQry);
    >         Object result = expr.evaluate(ansDoc, XPathConstants.NODESET);
    >
    > //Store the Results
    >         NodeList nodes = (NodeList) result;
    >
    > //Exit if there is no results found
    >         if(nodes.getLength() == 0){
    >             return root;
    >         }
    >
    > //Get ID for later use
    >         xpQry = "/answers/ques[@sequence='" + SeqNo + "' and
    > @item_id='" + queID + "']/@id";
    >         expr = xpath.compile(xpQry);
    >         result = expr.evaluate(ansDoc, XPathConstants.NODESET);
    >         NodeList ansID = (NodeList) result;
    >
    > //Does not append anything
    >         for(int nCnt=0;nCnt==nodes.getLength();nCnt++){
    >             Element answ = (Element) nodes.item(nCnt);
    >
    >             root.appendChild(answ);
    >         }
    >
    >         return root;
    >     }
    > ===
    >
    > Sample Data:
    > ---
    > <?xml version='1.0'?>
    >
    > <answers>
    >     <ques id='9447FCB8-1F77-422A-8FDA-7713CC7AE289' item_id='3BDF6103-
    > FC8C-4697-B93F-A49BAAAC97F2' sequence='0'>
    >         <answ answered='2009-03-02 13:23:00.857' device='system'
    > personnel='DTS, DTS'><![CDATA[Hall/Foyer]]></answ></ques></answers>
    > ===
    >
    > Ideally, I'd like to use XPath to query the structure for the node to
    > be appended onto another document.  But I don't want to have to resort
    > to searching line by line for the node I need as it may not at the
    > speed in which I seek.  However, I'm open to other suggestions.
    >
    > Thanks
    > Dominc


    For posterity:
    the solution is
    for(int nCnt=0;nCnt<nodes.getLength();nCnt++){
    Element answ = (Element) nDoc.importNode((Element)
    nodes.item(nCnt), true);

    root.appendChild(answ);
    }
     
    , Mar 4, 2009
    #2
    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. asd
    Replies:
    3
    Views:
    458
    Arnaud Berger
    May 23, 2005
  2. O.B.
    Replies:
    1
    Views:
    1,432
  3. Replies:
    1
    Views:
    614
    Soren Kuula
    Aug 24, 2006
  4. Replies:
    0
    Views:
    970
  5. Stanislaw Wozniak
    Replies:
    7
    Views:
    354
    Matt Neuburg
    Aug 3, 2009
Loading...

Share This Page