我有一个内联 HTML 对象 (com.google.gwt.user.client.ui.InlineHTML
),我想递归遍历节点元素以查找“输入”节点(请参阅下面)并获取它的类型(在下面的示例中 - 一个复选框),但是似乎我尝试的所有内容似乎都不想通过使用节点和元素来工作,但不断遇到问题..
有人对我如何做到这一点有任何建议 - 或者更好的是,给我指出一些代码?
谢谢
`
<p class="x-date">2010</p>
<div class="x-img-atv"></div>
<div class="x-actions">
<ul>
<li><a href="#" xhref="SS"> <span class="x-sprite delete">delete</span>delete</a></li>
<input type="checkbox">
<li><a href="#" xhref=""> <span class="x-sprite email">email</span>email</a></li>
<li><a href="#" xhref="XSS"> <span class="x-sprite review">review</span>review</a></li>
</ul>
</div>
</div>
`
最佳答案
GWT 具有集成的 XML parser 。它可用于将有效的 XML 元素转换为其节点对象,您可以在其中导航到正确的位置或正确的元素。
以下是如何使用它的示例:
package XXXX.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;
public class XMLparser_ implements EntryPoint {
public void onModuleLoad() {
//a really simple XML sample
final String xmlString = "<div><p>helle</p><p>World</p></div>";
Document xml = XMLParser.parse(xmlString);
//get the nodelist of the Document element can be more than one
NodeList nodesRoot = xml.getChildNodes();
for (int i = 0; i < nodesRoot.getLength(); i++) {
Node node = nodesRoot.item(i);
RootPanel.get().add(new Label(node.toString()));
//get the childs of the first node
NodeList nodesChild = node.getChildNodes();
for (int j = 0; j < nodesChild.getLength(); j++) {
Node node2 = nodesChild.item(j);
RootPanel.get().add(new Label(node2.toString()));
}
}
SafeHtml safeHTML = new SafeHtml() {
@Override
public String asString() {
return xmlString;
}
};
RootPanel.get().add(new HTML(safeHTML));
}
}
这是结果:
<div><p>helle</p><p>World</p></div>
<p>helle</p>
<p>World</p>
helle
World
关于java - 向下遍历 DOM 节点/元素是 GWT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9034764/