使用java的Xml条件解析
我想从XML文件中提取唯一选定的标记:使用java的Xml条件解析,java,xml,Java,Xml,我想从XML文件中提取唯一选定的标记: <Shape> <ShapeType>H2</ShapeType> <Annotation> <Properties> <PropertyValue PropertyName="field_label">label.modelSeriesCd</PropertyValue> <Proper
<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>
<Shape>
<ShapeType>H2</ShapeType>
<Annotation>
<Properties>
<PropertyValue PropertyName="field_label">label.modelSeriesMd</PropertyValue>
<PropertyValue PropertyName="ContainerType">mContainer</PropertyValue>
</Properties>
</Annotation>
<FootnoteNumber>1</FootnoteNumber>
<Name>label.modelSeriesCd</Name>
<Rectangle>
<Rectangle X="14" Y="94" Width="43" Height="12" />
</Rectangle>
</Shape>
氢
label.modelSeriesCd
条件容器
1.
label.modelSeriesCd
氢
label.modelSeriesMd
mContainer
1.
label.modelSeriesCd
我只想提取那些将“conditionContainer”作为“propertyValue”值的标记以及标记中的所有标记
我正在尝试以下代码:
private static void visitChildNodes(NodeList nList)
{
for (int index = 0; index < nList.getLength(); index++)
{
Node node = nList.item(index);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
if(node.getNodeName().equalsIgnoreCase("shape"))
System.out.println("Node Name = " + node.getNodeName() + "; Value = " + node.getTextContent());
私有静态无效访问childNodes(节点列表nList)
{
对于(int index=0;index
请给我一个方法。我建议通过您的
文档
对象进行递归搜索,因为您要查找的是几层深度
创建一个递归调用自身的函数,传递您当前所在的节点、正在查找的标记以及该标记必须具有的值
类似于
public static void main(String[] args) throws Exception {
String xml
= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<Shapes>\n"
+ " <Shape>\n"
+ " <ShapeType>H2</ShapeType>\n"
+ " <Annotation>\n"
+ " <Properties>\n"
+ " <PropertyValue PropertyName=\"field_label\">label.modelSeriesCd</PropertyValue>\n"
+ " <PropertyValue PropertyName=\"ContainerType\">conditionContainer</PropertyValue>\n"
+ " </Properties>\n"
+ " </Annotation>\n"
+ " <FootnoteNumber>1</FootnoteNumber>\n"
+ " <Name>label.modelSeriesCd</Name>\n"
+ " <Rectangle>\n"
+ " <Rectangle X=\"14\" Y=\"94\" Width=\"43\" Height=\"12\" />\n"
+ " </Rectangle>\n"
+ " </Shape>\n"
+ " <Shape>\n"
+ " <ShapeType>H2</ShapeType>\n"
+ " <Annotation>\n"
+ " <Properties>\n"
+ " <PropertyValue PropertyName=\"field_label\">label.modelSeriesMd</PropertyValue>\n"
+ " <PropertyValue PropertyName=\"ContainerType\">mContainer</PropertyValue>\n"
+ " </Properties>\n"
+ " </Annotation>\n"
+ " <FootnoteNumber>1</FootnoteNumber>\n"
+ " <Name>label.modelSeriesCd</Name>\n"
+ " <Rectangle>\n"
+ " <Rectangle X=\"14\" Y=\"94\" Width=\"43\" Height=\"12\" />\n"
+ " </Rectangle>\n"
+ " </Shape>\n"
+ "</Shapes>";
Document xmlDocument = DocumentBuilderFactory
.newInstance()
.newDocumentBuilder()
.parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));
Node node = findPropertyTagAndValue(xmlDocument.getFirstChild(), "PropertyValue", "conditionContainer");
if (node != null) {
System.out.println("Node Name = " + node.getNodeName() + "; Value = " + node.getTextContent());
}
}
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);
}
}
阅读这个问题-里面没有提到如何迭代或搜索。代码中的“if(node.getNodeName().equalsIgnoreCase(“shape”)”行可以查找“shape”标记,但..我不清楚如何在内部读取其他标记…如xml所示,标记中有几个标记,我的要求是查找“conditionContainer”并避免使用所有其他标记
Node Name = PropertyValue; Value = conditionContainer