0
我有我想要解析的XML响应。而我似乎也不过工作,我想知道,如何(在Java代码),我可以知道我已经达到了父节点如何检查您是否已到达父节点的最后一个孩子Java
XML的lastChild:
<Data>
<Lambda>Test</Lambda>
<Gr>Function</Gr>
<Approach>Method</Approach>
<Sentence>
<Text id="1">You are tall because </Text>
<Entry id="2">ApplicableConditions</Entry>
<Text id="3">.</Text>
</Sentence>
</Data>
代码:
String sentence = new String();
List<String> sentList = new ArrayList<>();
sentence += node.getTextContent();
// If last sibling and no children, then put current sentence into list
if(!node.hasChildNodes() && !node.getLastChild().hasChildNodes()) {
sentList.add(sentence);
}
例如,当当前节点是文章ID = 3,我如何检查,看看这确实是父节点句子的最后一个孩子?这样我可以将构造的句子添加到列表中并在稍后阅读。
这样,我将在发送列表中的以下字符串项:
你是高大的,因为ApplicableConditions。
编辑:
这第二个<Data>
<Lambda>Test</Lambda>
<Gr>Function</Gr>
<Approach>Method</Approach>
<Sentence>
<Text id="1">You are tall because </Text>
<Entry id="2">ApplicableConditions</Entry>
<Text id="3">.</Text>
</Sentence>
</Data>
<Data>
<Lambda>Test2</Lambda>
<Gr>Fucntion</Gr>
<Approach>Method</Approach>
<Sentence>
<Text id="1">Because you don't have any qualifying dependents and you are outside the eligible age range, </Text>
<Entry id="2">you don't qualify for this credit.</Text>
<BulletedList id="3">
<QuestionEntry id="4">
<Role>Condition</Role>
</QuestionEntry>
</BulletedList>
</Sentence>
</Data>
通知,结构略有不同...如何采取不同的结构考虑。我的解决方案似乎没有在这里工作......因为句子的最后一个孩子没有任何属性。也许更好使用Xpaths?
这是什么都用正则表达式来呢? – Laurel
这是一个stackoverflow推荐:) –
不要添加标签,除非它们与你的问题相关。 – Laurel