[jdom-interest] Problem with parsing two documents

Wang K H khong.hai.wang at gridnode.com
Thu Jan 18 17:11:30 PST 2001


Hi,

Here's my code and I'm using Jdom Beta 5. Thanks in advance for any help.

import java.util.*;
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;

public class XMLList
{
  final static private String PARSER =
"org.apache.xerces.parsers.SAXParser";

  private Vector  list;
  private Element lookupRoot;

  public XMLList()
  {
  }

  public List createList(String fullFilePath, String lookupFilePath)
  {
    File fileObj = new File(fullFilePath);
    File lookupFileObj = new File(lookupFilePath);
    return createList(lookupFileObj, lookupFileObj);
  }

  public List createList(File fileObj, File lookupFileObj)
  {
    log("createList");
    List displayList = null;
    try
    {
      SAXBuilder docBuilder = new SAXBuilder(PARSER, false);
      Document fileDoc = docBuilder.build(fileObj);
      Element fileRoot = fileDoc.getRootElement();

      SAXBuilder docBuilder2 = new SAXBuilder(PARSER, false);
      Document lookupDoc = docBuilder2.build(lookupFileObj);
      lookupRoot = lookupDoc.getRootElement();

      displayList = readFiles(fileRoot);
    }
    catch(Exception e)
    {
      log("Exception in reading files");
      e.printStackTrace(System.out);
    }
    return displayList;
  }

  private List readFiles(Element userFile)
  {
    log("readFiles");
    this.list = new Vector();
    int level = 0;
    List userfileChildren = userFile.getChildren();
    createListRows(userfileChildren, level);
    return this.list;
  }

  private void createListRows(List children, int currentLevel)
  {
    log("createListRows:Current level = " + currentLevel);
    Iterator cIterator = children.iterator();
    while(cIterator.hasNext())
    {
      Element child = (Element)cIterator.next();
      String tag = child.getName();
      log("Child Tag = " + tag);
      Element foundLookup = lookupRoot.getChild(tag);
      ListRow newListRow = createListRow(child, foundLookup, currentLevel);
      printListRow(newListRow);
      this.list.add(newListRow);
      if(child.hasMixedContent())
      {
        List grandChildren = child.getChildren();
        createListRows(grandChildren, ++currentLevel);
      }
    }
  }

  private ListRow createListRow(Element inputChild, Element foundLookup, int
currentLevel)
  {
    log("createListRow");
    ListRow listRow = new ListRow();
    listRow.setValue(inputChild.getTextTrim());
    if(foundLookup != null)
    {
      listRow.setLabel(foundLookup.getChildTextTrim("Label"));
      listRow.setAttr(foundLookup.getChildTextTrim("Attr"));
      listRow.setLookupFilename(foundLookup.getChildTextTrim("LookupFile"));
    }
    return listRow;
  }

  private void printListRow(ListRow listRow)
  {
    log("$$$$$Printing listRow");
    log("$$$$$Attr\t\t\t= " + listRow.getAttr());
    log("$$$$$Label\t\t= " + listRow.getLabel());
    log("$$$$$LookupFilename\t= " + listRow.getLookupFilename());
    log("$$$$$Value\t\t= " + listRow.getValue());
    log("$$$$$Printing listRow End");
  }

  private void log(String msg)
  {
    System.out.println("[XMLList] " + msg);
  }
}
----- Original Message -----
From: "Jason Hunter" <jhunter at collab.net>
To: "Wang K H" <khong.hai.wang at gridnode.com>
Cc: <jdom-interest at jdom.org>
Sent: Friday, January 19, 2001 2:24 AM
Subject: Re: [jdom-interest] Problem with parsing two documents


> You don't say what version of JDOM you're using.  Make sure you try the
> latest.
>
> If you still see the behavior, I suggest you simplify your code down to
> a small test case and submit the code demonstrating the oddity here.
>
> -jh-
>
> > Wang K H wrote:
> >
> > Hi,
> >
> > I am facing a problem with JDom. I am using the SAXBuilder to build 2
> > documents one after the other. Later in the program, when I am
> > manipulating the document using the getChildren() method from Element
> > class, I am getting a list of elements from the other document. Is
> > this possible? I had verified my code that I did not get the list
> > using a wrong element. What could be a possible cause?
> >
> > Thanks in advance for any reply.
> >
> > Thanks,
> > WKH
> _______________________________________________
> To control your jdom-interest membership:
>
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com
>




More information about the jdom-interest mailing list