解析嵌套标记xml java有困难
我正在解析字典api中的定义。我有这行xml解析嵌套标记xml java有困难,java,xml,dom,Java,Xml,Dom,我正在解析字典api中的定义。我有这行xml <dt>:any of a small genus (<it>Apteryx</it>) of flightless New Zealand birds with rudimentary wings, stout legs, a long bill, and grayish brown hairlike plumage</dt> 其中,def是容纳dt元件的元件 这是我的getValue代码 priv
<dt>:any of a small genus (<it>Apteryx</it>) of flightless New Zealand birds with rudimentary wings, stout legs, a long bill, and grayish brown hairlike plumage</dt>
其中,def是容纳dt元件的元件
这是我的getValue代码
private static String getValue(String tag, Element element)
{
NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = (Node) nodes.item(0);
return node.getNodeValue();
}
有时dt元素中有多个嵌套标记,我们得到:
结果:
:any of a small genus (<it>Apteryx</it>) of flightless New Zealand birds...
:新西兰不会飞的鸟类的一个小属的任何一种。。。
希望这有助于…为什么要使用“getValue”方法,难道不能使用def.getElementsByTagName(“dt”)来获取整个“dt”元素吗?否,因为它返回一个节点列表def.getElementsByTagName(“dt”).item(0)?
public static String getInnerXml(Node node)
{
DOMImplementationLS lsImpl = (DOMImplementationLS)node.getOwnerDocument().getImplementation().getFeature("LS", "3.0");
LSSerializer lsSerializer = lsImpl.createLSSerializer();
lsSerializer.getDomConfig().setParameter("xml-declaration", false);
NodeList childNodes = node.getChildNodes();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < childNodes.getLength(); i++)
{
sb.append(lsSerializer.writeToString(childNodes.item(i)));
}
return sb.toString();
}
getInnerXml(document.getElementsByTagName("dt").item(0));
:any of a small genus (<it>Apteryx</it>) of flightless New Zealand birds...