XML parsing


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: XML parsing

  1. #1
    Join Date
    Mar 2004
    Posts
    635

    XML parsing

    I only need simple functionality to read and write. The file is standard.
    Code:
    <?xml version="1.0" encoding="UTF-8" ?>
    File is stored locally, not network needed to access the file's data. Should I look into SAX or is there a better alternative I should use?

  2. #2
    Join Date
    Aug 2003
    Posts
    313
    I think I read somewhere that it was the slowest of all the xml parsers for java but I have used the DOM parser. I haven't tried any other ones, if you give one a try you should tell me how it goes. Some tutorials for the DOM api are located at: http://java.sun.com/xml/tutorial_intro.html

    Hope this helps with getting started. If you find a better parser with good XPath support, post it up here. Thanks.
    ~evlich

  3. #3
    Join Date
    Dec 2003
    Location
    tx/us
    Posts
    131
    The DOM parser reads the whole file and generates an object tree. The SAX parser reads and tokenizes the XML stream and calls user-implemented methods when it encounters "interesting" tokens. The general wisdom is that you use the DOM parser for "small" documents and the SAX parser for "big" ones but one of my apps uses the DOM parser for rather large (>500K) XML import documents with no problem.

  4. #4
    Join Date
    Mar 2004
    Posts
    635
    500k is nothing compared to what my file is. Currently, its at 38mb and will only increase. For the most part, the file will generally only be accessed once, when the program is started. But there will be times when data is appended to it, no set limit on number of new entries at a time. If DOM is for smaller files, I'll read a bit more on SAX. I bought the Wrox "professional java xml" book which covers the following: SAX 2.0, DOM level 2, JAXP 1.1 and TrAX, JDOM, Apache XML, XLink, XPath, and various related topics. The book was released in 2001 and so may be a bit out-dated, but for only $5, I can't complain.

  5. #5
    Join Date
    Dec 2003
    Location
    tx/us
    Posts
    131
    Yes, SAX is probably going to be your best bet for a monster like that. Sounds like you've got a 75% Off Bookstore (nothing over $5) near you.

  6. #6
    Join Date
    Aug 2005
    Posts
    2

    adding new attribute to an existing xml file

    Hi

    This is my first post to this group and Im hoping to get some suggestions as Im completely exhausted trying to resolve my query.

    I have a test.xml file whose structure is such:

    <Top>
    <Tags>
    <Tag id="123" name="Home">
    <Value>Inner Text</Value>
    </Tag>
    </Tags>
    </Top>

    Now using the JAXP DocumentBuilder I am able to access the attribute @name from the <Tag> node. I then want to insert a new attribute @new="some value" to this <Tag> node. But despite trying out several options Im not able to. I want to do something similar with the <Value> node as well and append some InnerText to the existing one.

    My code so far is:

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(test.xml);

    NodeList list = doc.getElementsByTagName("Tag");

    for(int i=0; i<list.getLength();i++){
    Element tagNode = (Element)list.item(i); //<Tag>
    NamedNodeMap nm = tagNode.getAttributes();

    for(int j=0;j<nm.getLength();j++){
    Node ndNm = nm.item(j); // id, name --attributes names
    String nodeLocNm = ndNm.getNodeName();

    if(nodeLocNm.equals(new String("name"))){
    String nodeVal = nm.item(j).getNodeValue(); //@name
    String newRes = "new value";
    tagNode.setAttribute("new", "new value");
    }
    }
    }

    --------------------------------------------------

    Of course no attribute @name gets added to test.xml.

    Could someone please help.

    Thanks in advance to everyone for helping out!
    Rahil

  7. #7
    Join Date
    Mar 2004
    Posts
    635
    For future reference, you should create your own topic thread rather than hopping on the end of someone else's. It keeps things more organized that way.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center