You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xmlbeans-user@xml.apache.org by Michael Lancaster <m....@ph.unimelb.edu.au> on 2004/06/09 07:45:27 UTC

access to parent and child nodes

hi,

another (hopefully) quick question... is there a way to access the 
parent node from a given XmlObject, and/or a way to access the array of 
Child elements for a general XmlObject such as it is done in DOM?

newDOMNode(0) will clearly *not* suffice as it returns a copy of the 
xml rather than a live copy   :(

I am attempting to implement drag and drop in a JTree where the 
underlying data model is based on XmlBeans, and it is difficult to find 
a nice solution without such methods.

thanks,

michael


- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-user-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/


Re: access to parent and child nodes

Posted by nn <nn...@comcast.net>.
hi michael,

I implemented pretty much DOM like interface from XmlObject(for SAX and
XMLBean combination, it works now).
XMLBean generated class does not maintain one-to-one correspondance to DOM
structure, so this mapping must be done carefully.

Anyway to get node related information, you need to use XmlCursor.
In particular you can not rely on toFirstChild, toNextSibling to emulate
getChildren of DOM, it will skip Text child.
So you need to use toNextToken to travarse token and check the type. So the
current XmlObject implementation seems not appropriate for efficient DOM
interface implementation..

followings are the examples:

public Object getParent(Object node) {
 // assert trace(node, "getParent");
 if (node instanceof XmlObject) {
     XmlObject xmlObj = (XmlObject)node;
     XmlCursor xc = xmlObj.newCursor();
     try {
  if (!xc.toParent()) return null;
  return xc.getObject();
     } finally {
  xc.dispose();
     }
 } else if (node instanceof XBeanSimpleType) {
     return ((XBeanSimpleType)node).getParent();
 } else {
     return null;
 }
    }

    private static String[] token_name_table = {
 "NONE", // 0
 "STARTDOC", // 1
 "ENDDOC", // 2
 "START", // 3
 "END", // 4
 "TEXT", // 5
 "ATTR", // 6
 "NAMESPACE", // 7
 "COMMENT", // 8
 "PROCINST" // 9
    };
    private static String getTokenTypeName(int type) {
 return token_name_table[type];
    }
    public List getChildren(Object node) {
 // assert trace(node, "getChildren");
 if (node instanceof XmlObject) {
     XmlObject xmlObj = (XmlObject)node;
     // assert debug("==============");
     // assert debug(">> getChildren: "+xmlObj);
     XmlCursor xc = xmlObj.newCursor();
     List children = null;
     try {
  int e_idx = 1;
  while (xc.toNextToken() != TokenType.NONE) {
      int type = xc.currentTokenType().intValue();
      // assert debug(">> type: "+getTokenTypeName(type)+", e_idx:
"+e_idx+", class: "+getClass(xc.getObject())+", token: "+xc.getObject());
      switch (type) {
      case 0: { // TokenType.INT_NONE
   break;
      }
      case 1: { // TokenType.INT_STARTDOC
   break;
      }
      case 2: { // TokenType.INT_ENDDOC
   break;
      }
      case 3: { // TokenType.INT_START
   if (e_idx == 1) {
       // assert debug(">> element added");
       if (children == null) children = new ArrayList();
       children.add(xc.getObject());
   }
   e_idx++;
   break;
      }
      case 4: { // TokenType.INT_END
   e_idx--;
   if (e_idx == 0) {
       return (children == null)?Collections.EMPTY_LIST:children;
   }
   break;
      }
      case 5: { // TokenType.INT_TEXT
   if (e_idx == 1) {
       // assert debug(">> TEXT added");
       if (children == null) children = new ArrayList();
       children.add(new XBeanText(xc.getChars(), xmlObj));
   }
   break;
      }
      case 6: { // TokenType.INT_ATTR
   break;
      }
      case 7: { // TokenType.INT_NAMESPACE
   break;
      }
      case 8: { // TokenType.INT_COMMENT
   if (e_idx == 1) {
       // assert debug(">> COMMENT added");
       if (children == null) children = new ArrayList();
       children.add(new XBeanComment(xc.getTextValue(), xmlObj));
   }
   break;
      }
      case 9: { // TokenType.INT_PROCINST
   if (e_idx == 1) {
       // assert debug(">> PROCINST added");
       if (children == null) children = new ArrayList();
       children.add(new XBeanProcinst(xc.getTextValue(), xmlObj));
   }
   break;
      }
      default: {
   break;
      }
      }
  }
  return (children == null)?Collections.EMPTY_LIST:children;
     } finally {
  xc.dispose();
     }
 } else {
     return Collections.EMPTY_LIST;
 }
    }

nn


----- Original Message ----- 
From: "Michael Lancaster" <m....@ph.unimelb.edu.au>
To: <xm...@xml.apache.org>
Sent: Tuesday, June 08, 2004 10:45 PM
Subject: access to parent and child nodes


> hi,
>
> another (hopefully) quick question... is there a way to access the
> parent node from a given XmlObject, and/or a way to access the array of
> Child elements for a general XmlObject such as it is done in DOM?
>
> newDOMNode(0) will clearly *not* suffice as it returns a copy of the
> xml rather than a live copy   :(
>
> I am attempting to implement drag and drop in a JTree where the
> underlying data model is based on XmlBeans, and it is difficult to find
> a nice solution without such methods.
>
> thanks,
>
> michael
>
>
> - ---------------------------------------------------------------------
> To unsubscribe, e-mail:   xmlbeans-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xmlbeans-user-help@xml.apache.org
> Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
>


- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-user-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/


Re: access to parent and child nodes

Posted by nn <nn...@comcast.net>.
hi michael,

I implemented pretty much DOM like interface from XmlObject(for SAX and
XMLBean combination, it works now).
XMLBean generated class does not maintain one-to-one correspondance to DOM
structure, so this mapping must be done carefully.

Anyway to get node related information, you need to use XmlCursor.
In particular you can not rely on toFirstChild, toNextSibling to emulate
getChildren of DOM, it will skip Text child.
So you need to use toNextToken to travarse token and check the type. So the
current XmlObject implementation seems not appropriate for efficient DOM
interface implementation..

followings are the examples:

public Object getParent(Object node) {
 // assert trace(node, "getParent");
 if (node instanceof XmlObject) {
     XmlObject xmlObj = (XmlObject)node;
     XmlCursor xc = xmlObj.newCursor();
     try {
  if (!xc.toParent()) return null;
  return xc.getObject();
     } finally {
  xc.dispose();
     }
 } else if (node instanceof XBeanSimpleType) {
     return ((XBeanSimpleType)node).getParent();
 } else {
     return null;
 }
    }

    private static String[] token_name_table = {
 "NONE", // 0
 "STARTDOC", // 1
 "ENDDOC", // 2
 "START", // 3
 "END", // 4
 "TEXT", // 5
 "ATTR", // 6
 "NAMESPACE", // 7
 "COMMENT", // 8
 "PROCINST" // 9
    };
    private static String getTokenTypeName(int type) {
 return token_name_table[type];
    }
    public List getChildren(Object node) {
 // assert trace(node, "getChildren");
 if (node instanceof XmlObject) {
     XmlObject xmlObj = (XmlObject)node;
     // assert debug("==============");
     // assert debug(">> getChildren: "+xmlObj);
     XmlCursor xc = xmlObj.newCursor();
     List children = null;
     try {
  int e_idx = 1;
  while (xc.toNextToken() != TokenType.NONE) {
      int type = xc.currentTokenType().intValue();
      // assert debug(">> type: "+getTokenTypeName(type)+", e_idx:
"+e_idx+", class: "+getClass(xc.getObject())+", token: "+xc.getObject());
      switch (type) {
      case 0: { // TokenType.INT_NONE
   break;
      }
      case 1: { // TokenType.INT_STARTDOC
   break;
      }
      case 2: { // TokenType.INT_ENDDOC
   break;
      }
      case 3: { // TokenType.INT_START
   if (e_idx == 1) {
       // assert debug(">> element added");
       if (children == null) children = new ArrayList();
       children.add(xc.getObject());
   }
   e_idx++;
   break;
      }
      case 4: { // TokenType.INT_END
   e_idx--;
   if (e_idx == 0) {
       return (children == null)?Collections.EMPTY_LIST:children;
   }
   break;
      }
      case 5: { // TokenType.INT_TEXT
   if (e_idx == 1) {
       // assert debug(">> TEXT added");
       if (children == null) children = new ArrayList();
       children.add(new XBeanText(xc.getChars(), xmlObj));
   }
   break;
      }
      case 6: { // TokenType.INT_ATTR
   break;
      }
      case 7: { // TokenType.INT_NAMESPACE
   break;
      }
      case 8: { // TokenType.INT_COMMENT
   if (e_idx == 1) {
       // assert debug(">> COMMENT added");
       if (children == null) children = new ArrayList();
       children.add(new XBeanComment(xc.getTextValue(), xmlObj));
   }
   break;
      }
      case 9: { // TokenType.INT_PROCINST
   if (e_idx == 1) {
       // assert debug(">> PROCINST added");
       if (children == null) children = new ArrayList();
       children.add(new XBeanProcinst(xc.getTextValue(), xmlObj));
   }
   break;
      }
      default: {
   break;
      }
      }
  }
  return (children == null)?Collections.EMPTY_LIST:children;
     } finally {
  xc.dispose();
     }
 } else {
     return Collections.EMPTY_LIST;
 }
    }

nn


----- Original Message ----- 
From: "Michael Lancaster" <m....@ph.unimelb.edu.au>
To: <xm...@xml.apache.org>
Sent: Tuesday, June 08, 2004 10:45 PM
Subject: access to parent and child nodes


> hi,
>
> another (hopefully) quick question... is there a way to access the
> parent node from a given XmlObject, and/or a way to access the array of
> Child elements for a general XmlObject such as it is done in DOM?
>
> newDOMNode(0) will clearly *not* suffice as it returns a copy of the
> xml rather than a live copy   :(
>
> I am attempting to implement drag and drop in a JTree where the
> underlying data model is based on XmlBeans, and it is difficult to find
> a nice solution without such methods.
>
> thanks,
>
> michael
>
>
> - ---------------------------------------------------------------------
> To unsubscribe, e-mail:   xmlbeans-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xmlbeans-user-help@xml.apache.org
> Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
>


- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-user-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/