Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Elements by Tag Name from a Node in Android (XML) Document?

I have an XML like this:

  <!--...-->
  <Cell X="4" Y="2" CellType="Magnet">
    <Direction>180</Direction>
    <SwitchOn>true</SwitchOn>
    <Color>-65536</Color>
  </Cell>
  <!--...-->

There're many Cell elements, and I can get the Cell Nodes by GetElementsByTagName. However, I realise that Node class DOESN'T have GetElementsByTagName method! How can I get Direction node from that cell node, without go throught the list of ChildNodes? Can I get the NodeList by Tag name like from Document class?

Thank you.

like image 756
Luke Vo Avatar asked Jun 17 '11 03:06

Luke Vo


People also ask

How to get Element by tag HTML?

You access an element by tag with the getElementsByTagName() method. For our tag example, we're using article elements. Just like accessing an element by its class, getElementsByTagName() will return an array-like object of elements, and you can modify every tag in the document with a for loop.

What does get Element by tagname return?

getElementsByTagName() method returns a live HTMLCollection of elements with the given tag name. All descendants of the specified element are searched, but not the element itself. The returned list is live, which means it updates itself with the DOM tree automatically. Therefore, there is no need to call Element.

How do I check if a tag is present in XML?

To verify if node or tag exists in XML content, you can execute an xpath expression against DOM document for that XML and count the matching nodes. matching nodes > zero – XML tag / attribute exists. matching nodes <= zero – XML tag / attribute does not exist.

Why getElementsByTagName is not working?

The "getElementsByTagName is not a function" error occurs for multiple reasons: calling the getElementsByTagName() method on a value that is not a DOM element. placing the JS script tag above the code that declares the DOM elements. misspelling getElementsByTagName (it's case sensitive).


1 Answers

You can cast the NodeList item again with Element and then use getElementsByTagName(); from Element class. The best approach is to make a Cell Object in your project alongwith fields like Direction, Switch, Color. Then get your data something like this.

String direction [];
NodeList cell = document.getElementsByTagName("Cell");
int length = cell.getLength();
direction = new String [length];
for (int i = 0; i < length; i++)
{
    Element element = (Element) cell.item(i);
    NodeList direction = element.getElementsByTagName("Direction");

    Element line = (Element) direction.item(0);

    direction [i] = getCharacterDataFromElement(line);

    // remaining elements e.g Switch , Color if needed
}

Where your getCharacterDataFromElement() will be as follow.

public static String getCharacterDataFromElement(Element e)
{
    Node child = e.getFirstChild();
    if (child instanceof CharacterData)
    {
        CharacterData cd = (CharacterData) child;
        return cd.getData();
    }
    return "";
}
like image 98
Adil Soomro Avatar answered Oct 02 '22 23:10

Adil Soomro