Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > get a xml node as-it-is

Reply
Thread Tools

get a xml node as-it-is

 
 
ArunDhaJ
Guest
Posts: n/a
 
      01-17-2008
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
 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      01-19-2008
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.getD ocumentElement(),
NodeFilter.SHOW_ELEMENT, new Filter(), false);
Node n;
while((n = walk.nextNode()) != null) {

newdoc.getDocumentElement().appendChild(newdoc.imp ortNode(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("n umber").getNodeValue());
if(num >= 7 && num <= {
return NodeFilter.FILTER_ACCEPT;
} else {
return NodeFilter.FILTER_REJECT;
}
} else {
return NodeFilter.FILTER_REJECT;
}
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Reading XML Node-by-Node Martin Honnen ASP .Net 0 11-12-2008 06:29 PM
xsl variable $node/text() but $node can non-node-set help! Tjerk Wolterink XML 2 08-24-2006 03:28 AM
how to creating new node inside a xml node Geagleeye ASP General 2 08-03-2006 12:09 PM
How to set the node indent property between the parent node and the leaf node viveknatani@gmail.com ASP .Net 0 02-13-2006 07:11 PM
How to drag a sub-node to another node in TreeView? wolf ASP .Net 0 08-18-2004 11:51 PM



Advertisments