You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Yang Xiao <yx...@ohpp.com> on 2004/04/28 20:36:46 UTC
newbie question
Hi all,
I'm having some problems with parsing XML with JDOM.
What I want is to filter out particular elements in the XML, the sample XML
looks like this.
Thanks in advance.
Yang
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
<CD>
<TITLE>Greatest Hits</TITLE>
<ARTIST>Dolly Parton</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>RCA</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1982</YEAR>
</CD>
</CATALOG>
and my code(which doesn't work :-))
import java.io.IOException;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
// A Simple DOM Application
public class BasicDOM2 {
// Constructor
public BasicDOM2(String xmlFile) {
// Create a Xerces DOM Parser
DOMParser parser = new DOMParser();
// Parse the Document
// and traverse the DOM
try {
parser.parse(xmlFile);
Document document = parser.getDocument();
traverse(document);
} catch (SAXException e) {
System.err.println(e);
} catch (IOException e) {
System.err.println(e);
}
}
// Traverse DOM Tree. Print out Element Names
private void traverse(Node node) {
int type = node.getNodeType();
if (type == Node.ELEMENT_NODE &&
node.getNodeName().equals("ARTIST")){
System.out.print(node.getNodeName() + ": ");
System.out.print(node.getNodeValue());
process(node);
} else {
}
/*
if (type == Node.TEXT_NODE){// &&
node.getNodeName().equals("ARTIST"))
System.out.println(node.getNodeValue());
// System.out.println(node.getLocalName());
}else{
}
*/
NodeList children = node.getChildNodes();
if (children != null) {
for (int i=0; i< children.getLength(); i++)
traverse(children.item(i));
}
}
// Main Method
public static void main(String[] args) {
BasicDOM2 basicDOM = new BasicDOM2("c:/temp/cd2.xml");
}
public void process(Node node){
StringBuffer buf = new StringBuffer();
Node n = node;
//NodeList nl = textNode.getChildNodes();
//for( int i=0; i<nl.getLength(); i++ ){
// n = nl.item(i);
if( n.getNodeType() == Node.TEXT_NODE ) {
buf.append( n.getNodeValue() );
}else{
// expected a text-only node!
}
//}
System.out.println(buf.toString());
}
}
manipulate automata during validation
Posted by Lingzhi Zhang <lz...@cse.ogi.edu>.
Hi,
I am wondering whether there is a way to manipulate the schema automata
during the validation. Here is what I am thinking about: when the parser
sees a tag which is not validate against the schema, it then goes to
another state in the automata and the validation continues from that
state. Does Xerces allow such kind of configuration over the parser? If
there is any way to get started, where? Thanks a lot.
Best
Stephen
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org