gpt4 book ai didi

java - 使用java进行Xml条件解析

转载 作者:行者123 更新时间:2023-11-30 03:21:57 25 4
gpt4 key购买 nike

我想从 XML 文件中提取仅选定的标签:

<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>

我只想提取那些将“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());

请建议我一种方法来做到这一点。

最佳答案

我建议通过您的 Document 对象进行递归搜索,因为您要查找的内容有几层深度。

创建一个函数,递归地调用自身,传递您当前所在的节点以及您正在查找的标签以及该标签必须具有的值。

类似...

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);
}
}

结果:

Node Name = PropertyValue; Value = conditionContainer

关于java - 使用java进行Xml条件解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31137777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com