Forum Stats

  • 3,872,606 Users
  • 2,266,458 Discussions
  • 7,911,262 Comments

Discussions

Xerces or JDOM which is better?

843834
843834 Member Posts: 49,972
edited Jul 26, 2001 12:55PM in Java Technology & XML
hi all

i would like to ask for some opinions.
Xerces and JDOM which is better?
thanks!

Comments

  • 843834
    843834 Member Posts: 49,972
    Hi ,



    If you are programming in java, then i would recommend using jdom in lieu of dom or sax parsers, because it combines the best features of both: you get the performance of SAX and the object-tree of dom. Typically, DOM model is not suitable for parsing large documents due to memory restrictions. On the other hand, SAX parsers are fast but are sequential and although you can read the data, it does not provide a way to hange the data easily.

    However, you can use jdom's sax parser to get the speed and still get the object model so that you can modify data.



    JDOM is a unique Java toolkit for working with XML, engineered to enable rapid development of XML applications. Its design embraces the Java language from syntax to semantics. But is it better than existing -- and more standard -- XML APIs?

    Where JDOM fits :

    JDOM can be used as an alternative to the org.w3c.dom package for programmatically manipulating XML documents. It's not a drop-in replacement, and, in fact, JDOM and DOM can happily coexist. In addition, JDOM is not concerned with parsing XML from text input, although it provides wrapper classes that take much of the work out of configuring and running a parser implementation. JDOM builds on the strengths of existing APIs to build, as the project home page states, "a better mousetrap."


    JDOM is Java platform specific. The API uses the Java language's built-in String support wherever possible, so that text values are always available as Strings. It also makes use of the Java 2 platform collection classes, like List and Iterator, providing a rich environment for programmers familiar with the Java language.


    No hierarchies. In JDOM, an XML element is an instance of Element, an XML attribute is an instance of Attribute, and an XML document itself is an instance of Document. Because all of these represent different concepts in XML, they are always referenced as their own types, never as an amorphous "node."


    Class driven. Because JDOM objects are direct instances of classes like Document, Element, and Attribute, creating one is as easy as using the new operator in the Java language. It also means that there are no factory interfaces to configure -- JDOM is ready to use straight out of the jar.



    Hope this will help you.

    Regards,
    Anil.
    Technical Support Engineer.

  • 843834
    843834 Member Posts: 49,972
    This dude is right. JDOM6 is the bomb.
    here is a little present:
    import java.io.*;
    import java.io.File;
    import java.util.List;
    import java.util.Iterator;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.io.IOException;
    import java.io.OutputStream;

    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.JDOMException;
    import org.jdom.adapters.*;
    import org.jdom.adapters.CrimsonDOMAdapter;
    import org.jdom.input.DOMBuilder;
    import org.jdom.output.XMLOutputter;

    public class xmltest
    {
    // Global value so it can be ref'd by the tree-adapter
    public static Document document;
    public static DOMBuilder builder;
    public static String product_name, description, price;
    public static Double checkPrice;

    public static void readDoc()throws IOException, JDOMException
    {
    builder = new DOMBuilder();

    try {
    FileInputStream in = new FileInputStream(new File("settings.xml"));
    document = builder.build(in);
    in.close();
    } catch (IOException ioe) {
    ioe.printStackTrace();
    }
    }

    public static void listProducts()throws IOException, JDOMException
    {
    readDoc();
    Element root = document.getRootElement();
    List products = root.getChildren();
    Iterator productsIterator = products.iterator();
    System.out.println("Currently " + products.size() + " products.");
    while(productsIterator.hasNext()){
    Element productElement = (Element)productsIterator.next();
    List product = productElement.getChildren();
    System.out.println(productElement.getTextTrim());
    Iterator productIterator = product.iterator();
    while(productIterator.hasNext()){
    Element attElement = (Element)productIterator.next();
    System.out.println(attElement.getName() + " : " + attElement.getText());
    }
    }

    }
    public static void removeProduct(String productName)throws IOException, JDOMException
    {
    readDoc();
    Element root = document.getRootElement();
    List products = root.getChildren();
    Iterator productsIterator = products.iterator();
    root.removeChildren();
    while(productsIterator.hasNext()){
    Element tempElement = (Element)productsIterator.next();
    if(!tempElement.getTextTrim().equals(productName)){
    root.addContent(tempElement);
    }
    }
    document.setRootElement(root);
    saveChanges();
    }

    public static void addProduct(String product_name, String description, String price)throws IOException, JDOMException
    {
    readDoc();
    Element product = new Element("product");
    Element root = document.getRootElement();
    product.addContent(product_name);
    product.addContent(new Element("description").addContent(description));
    product.addContent(new Element("price").addContent(price).addAttribute("currency", "US"));
    root.addContent(product);
    saveChanges();
    }

    public static void saveChanges(){
    try {
    XMLOutputter outputter = new XMLOutputter();
    FileWriter writer = new FileWriter("settings.xml");
    outputter.output(document, writer);
    writer.close();
    } catch (java.io.IOException e) {
    e.printStackTrace();
    }
    }


    public static void main(String argv[])throws IOException, JDOMException
    {
    int selection;

    for(;;){
    System.out.println("1. Add a product\n2. Remove a product\n3. View products\n4. exit program");
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    try{
    selection = Integer.parseInt(in.readLine());
    switch(selection){
    case 1:
    System.out.println("Enter product name.");
    product_name = in.readLine();
    System.out.println("Enter product description.");
    description = in.readLine();
    System.out.println("Enter product price.");
    price = in.readLine();
    try{
    checkPrice = new Double(price);
    addProduct(product_name, description, price);
    }catch(Exception e){
    System.out.println("The product's price must be a numeric value.");
    }
    break;
    case 2:
    System.out.println("Enter product name to remove.");
    product_name = in.readLine();
    removeProduct(product_name);
    break;
    case 3:
    listProducts();
    break;
    case 4:
    System.exit(1);
    break;
    default:
    System.out.println("The number you have typed is incompatible with the menu.");
    break;
    }
    }catch(Exception e){
    System.out.println("Please type in a number that maches the menu.");
    }

    }
    }
    }

    enjoy....
  • 843834
    843834 Member Posts: 49,972
    will i be able to manipulate SVG with JDOM??
    like for example, open a SVG file add in a few elements etc...

    thanks
    xerokool
  • 843834
    843834 Member Posts: 49,972
    Have a look at

    Reading XML-
    http://www.javaworld.com/javaworld/jw-05-2000/jw-0518-jdom.html

    Manipulating XML -
    http://www.javaworld.com/javaworld/jw-07-2000/jw-0728-jdom2.html

    to see just how easy it is.
This discussion has been closed.