예제 #1
0
 public ArrayList<String> getChildNames() {
   ArrayList<String> a = new ArrayList<String>();
   for (SimpleXmlElement e : getChildren()) {
     a.add(e.getName());
   }
   return a;
 }
예제 #2
0
 public void removeChildren(String tagName) {
   for (Iterator<SimpleXmlElement> it = m_children.iterator(); it.hasNext(); ) {
     SimpleXmlElement e = it.next();
     if (e.getName().equalsIgnoreCase(tagName)) {
       it.remove();
     }
   }
 }
예제 #3
0
 public SimpleXmlElement getChild(String name) {
   for (SimpleXmlElement c : m_children) {
     if (c.getName().equalsIgnoreCase(name)) {
       return c;
     }
   }
   return null;
 }
예제 #4
0
 /** Returns an ArrayList with all children whose name matches the given regular expression. */
 public ArrayList<SimpleXmlElement> getChildrenRegEx(String childNameAsRegEx) {
   ArrayList<SimpleXmlElement> selectedChildren = new ArrayList<SimpleXmlElement>();
   for (SimpleXmlElement child : m_children) {
     if (child.getName().matches(childNameAsRegEx)) {
       selectedChildren.add(child);
     }
   }
   return selectedChildren;
 }
예제 #5
0
 public ArrayList<SimpleXmlElement> getChildren(String childrenName) {
   ArrayList<SimpleXmlElement> a = new ArrayList<SimpleXmlElement>();
   for (SimpleXmlElement c : m_children) {
     if (c.getName().equalsIgnoreCase(childrenName)) {
       a.add(c);
     }
   }
   return a;
 }
예제 #6
0
 public boolean isAncestorOf(SimpleXmlElement child) {
   SimpleXmlElement e = child;
   while (e != null) {
     if (e == this) {
       return true;
     }
     e = e.getParent();
   }
   return false;
 }
예제 #7
0
 public void replaceChild(SimpleXmlElement oldChild, SimpleXmlElement newChild) {
   if (newChild == null) {
     throw new IllegalArgumentException("newChild must not be null");
   }
   int index = m_children.indexOf(oldChild);
   if (index < 0) {
     throw new IllegalArgumentException("oldChild " + oldChild + " does not exist");
   }
   m_children.set(index, newChild);
   newChild.m_parent = this;
   oldChild.m_parent = null;
 }
예제 #8
0
  /**
   * Returns the first child that matches the given regular expression or null if there's no match.
   */
  public SimpleXmlElement getChildRegEx(String childNameAsRegEx) {
    Iterator<SimpleXmlElement> iterator = m_children.iterator();

    while (iterator.hasNext()) {
      SimpleXmlElement child = iterator.next();

      if (child.getName().matches(childNameAsRegEx)) {
        return child;
      }
    }

    return null;
  }
예제 #9
0
 public SimpleXmlElement getRoot() {
   if (m_parent == null) {
     return this;
   } else {
     return m_parent.getRoot();
   }
 }
예제 #10
0
 public void addChild(SimpleXmlElement child) {
   if (child.m_parent != null) {
     child.m_parent.removeChild(child);
   }
   m_children.add(child);
   child.m_parent = this;
 }
예제 #11
0
 protected void write(Writer writer, String prefix) throws IOException {
   if (m_name == null) {
     this.writeEncoded(writer, m_contents);
     return;
   }
   writer.write(prefix + '<');
   writer.write(m_name);
   if (!m_attributeMap.isEmpty()) {
     for (String key : m_attributeNames) {
       String value = getAttributeInternal(key);
       if (value != null) {
         writer.write(' ');
         writer.write(key);
         writer.write('=');
         writer.write('"');
         this.writeEncoded(writer, value);
         writer.write('"');
       }
     }
   }
   if (!m_children.isEmpty()) {
     writer.write('>');
     writer.write("\n");
     Iterator<?> en = this.getChildren().iterator();
     while (en.hasNext()) {
       SimpleXmlElement child = (SimpleXmlElement) en.next();
       child.write(writer, prefix + "  ");
     }
     writer.write(prefix + '<');
     writer.write('/');
     writer.write(m_name);
     writer.write('>');
     writer.write("\n");
   } else if ((m_contents != null) && (m_contents.length() > 0)) {
     writer.write('>');
     this.writeEncoded(writer, m_contents);
     writer.write('<');
     writer.write('/');
     writer.write(m_name);
     writer.write('>');
     writer.write("\n");
   } else { // this.children.isEmpty()
     writer.write('/');
     writer.write('>');
     writer.write("\n");
   }
 }
예제 #12
0
 public void removeChild(SimpleXmlElement child) {
   m_children.remove(child);
   child.m_parent = null;
 }
예제 #13
0
 public void addChild(SimpleXmlElement child, int pos) {
   m_children.add(pos, child);
   child.m_parent = this;
 }
예제 #14
0
 protected void scanElement(SimpleXmlElement elt) throws IOException {
   StringBuffer buf = new StringBuffer();
   this.scanIdentifier(buf);
   String eltName = buf.toString();
   elt.setName(eltName);
   char ch = this.scanWhitespace();
   while ((ch != '>') && (ch != '/')) {
     buf.setLength(0);
     this.unreadChar(ch);
     this.scanIdentifier(buf);
     String key = buf.toString();
     ch = this.scanWhitespace();
     if (ch != '=') {
       throw this.expectedInput("=");
     }
     this.unreadChar(this.scanWhitespace());
     buf.setLength(0);
     this.scanString(buf);
     elt.setAttribute(key, buf);
     ch = this.scanWhitespace();
   }
   if (ch == '/') {
     ch = this.readChar();
     if (ch != '>') {
       throw this.expectedInput(">");
     }
     return;
   }
   buf.setLength(0);
   ch = this.scanWhitespace(buf);
   if (ch != '<') {
     this.unreadChar(ch);
     this.scanPCData(buf);
   } else {
     for (; ; ) {
       ch = this.readChar();
       if (ch == '!') {
         if (this.checkCDATA(buf)) {
           this.scanPCData(buf);
           break;
         } else {
           ch = this.scanWhitespace(buf);
           if (ch != '<') {
             this.unreadChar(ch);
             this.scanPCData(buf);
             break;
           }
         }
       } else {
         if ((ch != '/') || m_ignoreWhitespace) {
           buf.setLength(0);
         }
         if (ch == '/') {
           this.unreadChar(ch);
         }
         break;
       }
     }
   }
   if (buf.length() == 0) {
     while (ch != '/') {
       if (ch == '!') {
         ch = this.readChar();
         if (ch != '-') {
           throw this.expectedInput("Comment or Element");
         }
         ch = this.readChar();
         if (ch != '-') {
           throw this.expectedInput("Comment or Element");
         }
         this.skipComment();
       } else {
         this.unreadChar(ch);
         SimpleXmlElement child = this.createAnotherElement();
         this.scanElement(child);
         elt.addChild(child);
       }
       ch = this.scanWhitespace();
       if (ch != '<') {
         throw this.expectedInput("<");
       }
       ch = this.readChar();
     }
     this.unreadChar(ch);
   } else {
     if (m_ignoreWhitespace) {
       elt.setContent(buf.toString().trim());
     } else {
       elt.setContent(buf.toString());
     }
   }
   ch = this.readChar();
   if (ch != '/') {
     throw this.expectedInput("/");
   }
   this.unreadChar(this.scanWhitespace());
   if (!this.checkLiteral(eltName)) {
     throw this.expectedInput(eltName);
   }
   if (this.scanWhitespace() != '>') {
     throw this.expectedInput(">");
   }
 }