以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 Java/Eclipse 』  (http://bbs.xml.org.cn/list.asp?boardid=41)
----  [原创]xml提取  (http://bbs.xml.org.cn/dispbbs.asp?boardid=41&rootid=&id=84311)


--  作者:sfe105
--  发布时间:4/15/2010 5:19:00 PM

--  [原创]xml提取
对于给定元素名,如何把这个元素下指定的属性名的内容提取出来
--  作者:sfe105
--  发布时间:4/15/2010 5:27:00 PM

--  
是不是就是对其子元素的操作
--  作者:mfc42d
--  发布时间:5/17/2010 8:09:00 PM

--  
5.查找指定名的Element(一个)
/**
* Gets the first child element with the given name or returns null if one can't be found.
* @param parent the parent element of the child element to search for
* @param childName the name of the child element
* @param deepSearch - if True then the search will be performed on all levels
* If False then only Direct childs will be searched
* @return the first child element with the given name or null if one can't be found.
*/
public static Element getFirstChildElementNamed(Element parent, String childName, boolean deepSearch)
{
   if (parent == null)
   {
     throw new NullPointerException("Parent element cannot be null");
   }
   NodeList children = parent.getChildNodes();
   Element child = null;
   for (int i = 0; i < children.getLength() && child == null; i++)
   {
     if (children.item(i).getNodeName().equals(childName))
     {
       child = (Element)children.item(i);
     }
     else if ((deepSearch) && (children.item(i).getNodeType() == Element.ELEMENT_NODE))
     {
       child = getFirstChildElementNamed((Element)children.item(i), childName, deepSearch);
     }
   }
   return child;
}
5.查找指定名的Element(一组)
/**
* Gets any child elements with the given name or returns null if one can't be found.
* @param parent the parent element of the child element to search for
* @param childName the name of the child element
* @param deepSearch - if True then the search will be performed on all levels
* If False then only Direct chileds will be searched
* @return the first child element with the given name or null if one can't be found.
*/
public static Element[] getAllChildElementNamed(Element parent, String childName, boolean deepSearch)
{
   if (parent == null)
   {
     throw new NullPointerException("Parent element cannot be null");
   }
   NodeList children = parent.getChildNodes();
   ArrayList child = new ArrayList();
   for (int i = 0; i < children.getLength(); i++)
   {
     if (children.item(i).getNodeName().equals(childName))
     {
       child.add(children.item(i));
     }
     else if ((deepSearch) && (children.item(i).getNodeType() == Element.ELEMENT_NODE))
     {
       Element[] childs = getAllChildElementNamed((Element)children.item(i), childName, deepSearch);
       for (int j=0; j< childs.length; j++)
       {
         child.add(childs[j]);
       }
     }
   }
   return (Element[])child.toArray(new Element[0]);
}

W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
46.875ms