get a xml node as-it-is

Discussion in 'Java' started by ArunDhaJ, Jan 17, 2008.

  1. ArunDhaJ

    ArunDhaJ Guest

    i hve a xml like this:
    <Students>
    <Student name = "arun1" number="6">
    <Student name = "arun2" number="7">
    <Student name = "arun3" number="8">
    <Student name = "arun4" number="9">
    ....
    ....
    ....
    </Students>

    i need to build a new xml or a string from this with few student
    nodes.... some thing like this..

    <Student>
    <Student name = "arun2" number="7">
    <Student name = "arun3" number="8">
    </Student>

    by selecting only the two nodes.........

    is there any api which should return some thing like this
    "<Student name = "arun2" number="7">"

    Thanks
    ArunDhaJ
     
    ArunDhaJ, Jan 17, 2008
    #1
    1. Advertising

  2. ArunDhaJ

    Arne Vajhøj Guest

    ArunDhaJ wrote:
    > i hve a xml like this:
    > <Students>
    > <Student name = "arun1" number="6">
    > <Student name = "arun2" number="7">
    > <Student name = "arun3" number="8">
    > <Student name = "arun4" number="9">
    > ...
    > ...
    > ...
    > </Students>
    >
    > i need to build a new xml or a string from this with few student
    > nodes.... some thing like this..
    >
    > <Student>
    > <Student name = "arun2" number="7">
    > <Student name = "arun3" number="8">
    > </Student>
    >
    > by selecting only the two nodes.........
    >
    > is there any api which should return some thing like this
    > "<Student name = "arun2" number="7">"


    It can be done in many different ways: getElementsByTagName,
    XPath etc., but just for the fun of it I tried using one
    of the less known features of W3C DOM - the Walker. See code
    below.

    Arne

    ==================================================

    import java.io.File;
    import java.io.FileOutputStream;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;

    import org.apache.xml.serialize.OutputFormat;
    import org.apache.xml.serialize.XMLSerializer;
    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.traversal.DocumentTraversal;
    import org.w3c.dom.traversal.NodeFilter;
    import org.w3c.dom.traversal.TreeWalker;

    public class Selection {
    public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new File("C:\\stud.xml"));
    Document newdoc = db.newDocument();
    newdoc.appendChild(newdoc.createElement("Students"));
    TreeWalker walk =
    ((DocumentTraversal)doc).createTreeWalker(doc.getDocumentElement(),
    NodeFilter.SHOW_ELEMENT, new Filter(), false);
    Node n;
    while((n = walk.nextNode()) != null) {

    newdoc.getDocumentElement().appendChild(newdoc.importNode(n, true));
    }
    OutputFormat fmt = new OutputFormat();
    fmt.setIndenting(true);
    XMLSerializer ser = new XMLSerializer(new
    FileOutputStream("C:\\stud2.xml"), fmt);
    ser.serialize(newdoc);
    }
    }

    class Filter implements NodeFilter {
    public short acceptNode(Node n) {
    if(n.getNodeName().equals("Student")) {
    int num =
    Integer.parseInt(n.getAttributes().getNamedItem("number").getNodeValue());
    if(num >= 7 && num <= 8) {
    return NodeFilter.FILTER_ACCEPT;
    } else {
    return NodeFilter.FILTER_REJECT;
    }
    } else {
    return NodeFilter.FILTER_REJECT;
    }
    }
    }
     
    Arne Vajhøj, Jan 19, 2008
    #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. wolf
    Replies:
    0
    Views:
    558
  2. Replies:
    0
    Views:
    1,470
  3. Tjerk Wolterink
    Replies:
    2
    Views:
    1,440
    Dimitre Novatchev
    Aug 24, 2006
  4. Martin Honnen

    Re: Reading XML Node-by-Node

    Martin Honnen, Nov 12, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    389
    Martin Honnen
    Nov 12, 2008
  5. Geagleeye

    how to creating new node inside a xml node

    Geagleeye, Aug 2, 2006, in forum: ASP General
    Replies:
    2
    Views:
    200
    Geagleeye
    Aug 3, 2006
Loading...

Share This Page