作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试遍历 XML 标签,并且使用 list.item(i).getFirstChild();
但它返回:[# text: ]。据我所知,此方法应该返回“标记名称”或 null
,但这里它返回备用标记名称和 [# text: ]即
[# 文本:]
标签名
[# 文本:]
标签名我正在使用的代码是:
public void readXML() throws ParserConfigurationException, SAXException, IOException, TransformerException
{
String rootNode=null;
//Get Document Builder
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Transformer tx = TransformerFactory.newInstance().newTransformer();
tx.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("C:/Users/ve00p5199/Desktop/xml/test.xml"));
NodeList list = doc.getElementsByTagName("*");
for (int i = 0; i < list.getLength(); i++) {
//System.out.println(list.item(i).getNodeName());
if(list.item(i).getNodeName().equalsIgnoreCase("shape")||list.item(i).getNodeName().equalsIgnoreCase("Textbox")||list.item(i).getNodeName().equalsIgnoreCase("Button")){
// System.out.println(list.item(i).getNodeName());
Node node = findPropertyTagAndValue(list.item(i).getFirstChild(), "PropertyValue", "conditionContainer");
if (node != null) {
System.out.println("Node Name = " + node.getNodeName() + "; Value = " + node.getTextContent());
DOMSource src = new DOMSource(list.item(i));
StringWriter sr = new StringWriter();
Result res = new StreamResult(sr);
tx.transform(src, res);
System.out.println(sr);
}
}
}
}
public static Node findPropertyTagAndValue(Node node, String propertyTag, String propertyValue) {
if (node == null) {
// The node we're looking for does not exist
return null;
} else if (node.getNodeType() != Node.ELEMENT_NODE) {
// Move to the next sibling node
return findPropertyTagAndValue(node.getNextSibling(), propertyTag, propertyValue);
} else if (node.getNodeName().equalsIgnoreCase(propertyTag) && node.getTextContent().equalsIgnoreCase(propertyValue)) {
// We found the node we are looking for
return node;
} else if (node.hasChildNodes()) {
// Check into the child nodes
Node childNode = findPropertyTagAndValue(node.getFirstChild(), propertyTag, propertyValue);
if (childNode == null) {
// Nothing found in child node, so move to next sibling
childNode = findPropertyTagAndValue(node.getNextSibling(), propertyTag, propertyValue);
}
return childNode;
} else {
// Move to the next sibling
return findPropertyTagAndValue(node.getNextSibling(), propertyTag, propertyValue);
}
}
我的 XML 是:
<Diagram>
<Widgets>
<Shape>
<ShapeType>H2</ShapeType>
<Annotation>
<Properties>
<PropertyValue PropertyName="field_label">label.modelSeriesCd</PropertyValue>
<PropertyValue PropertyName="ContainerType">conditionContainer</PropertyValue>
</Properties>
</Annotation>
<FootnoteNumber>1</FootnoteNumber>
<Name>label.modelSeriesCd</Name>
<Rectangle>
<Rectangle X="14" Y="94" Width="43" Height="12" />
</Rectangle>
</Shape>
</Diagram>
</Widgets>
请解释一下...
最佳答案
NodeList list = object.getElementsByTagName("*");
for (int i = 0; i < list.getLength(); i++) {
Node listNode = nodeList.item(i);
if (Node.ELEMENT_NODE == listNode.getNodeType()) {
String nodeName = listNode.getNodeName();
if (nodeName.equalsIgnoreCase("shape")
|| nodeName.equalsIgnoreCase("Textbox")
|| nodeName.equalsIgnoreCase("Button")) {
Node node = findPropertyTagAndValue(listNode, "PropertyValue", "conditionContainer");
if (node != null) {
System.out.println("Node Name = " + node.getNodeName() + "; Value = " + node.getTextContent());
DOMSource src = new DOMSource(list.item(i));
StringWriter sr = new StringWriter();
Result res = new StreamResult(sr);
tx.transform(src, res);
System.out.println(sr);
}
}
}
}
/**
* No Need to check for all those things. Only * will return all elements
* including #text node. If you give proper node name then it will return
* those node's only That's why there's node need for checking ELEMENT_NODE
* in finding propertTags
*/
public static Node findPropertyTagAndValue(Node node, String propertyTag, String propertyValue) {
if (Node.ELEMENT_NODE == node.getNodeType()) {
NodeList nodeList = ((Element) node).getElementsByTagName(propertyTag);
for (int i = 0; i < nodeList.getLength(); i++) {
Node listNode = nodeList.item(i);
if (listNode.getTextContent().equalsIgnoreCase(propertyValue)) {
return listNode;
}
}
}
return null;
}
关于java - 使用java : list. item(i).getFirstChild()遍历xml DOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31152747/
我是一名优秀的程序员,十分优秀!