[英]How to parse and put together pieces of text from an XML response Java
I have the following XML response: 我有以下XML响应:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<MetaData>
<xpath>/Temporary/EIC/HaveInaccurateInfo</xpath>
<enumeration>AtLeastOneConditionTrue</enumeration>
<scenario>TRUE_BECAUSE_OF_ONE_CONDITION</scenario>
<Template>
<Text id="1">You don't qualify because </Text>
<PertinentDataInputNodeNameListInline id="2"
>ApplicableConditions</PertinentDataInputNodeNameListInline>
<Text id="3">.</Text>
</Template>
</MetaData>
<MetaData>
<xpath>/Temporary/EIC/DisqualifiedBecauseAllQualifyingChildrenHaveITIN</xpath>
<scenario>DISQUALIFIED</scenario>
<Template>
<Text id="1">Your eligibility for this credit is not affected since </Text>
<PertinentDataInputNodeNameListInline id="2">ApplicableConditions</PertinentDataInputNodeNameListInline>
<Text id="3">.</Text>
</Template>
</MetaData>
</data>
I'd like to be able to write some java class to be able to combine/construct the text nodes under the Template
node, when I pass in an xpath
and scenario
(that way we'll know which Template to use). 我希望能够编写一些Java类,以便在传递xpath
和scenario
时能够组合/构造Template
节点下的文本节点(这样我们就知道要使用哪个Template)。
Example: 例:
public String constructSentence(String xpath, String scenario) {
// some processing here
return constructedSentence;
}
output: 输出:
You don't qualify because ApplicableConditions. 您没有资格,因为有适用条件。
etc... 等等...
How can I accomplish this using Java? 如何使用Java完成此操作? What is the best approach? 最好的方法是什么? Any recommendations? 有什么建议吗? I've heard many many times using regex to parse xml would be a sin, I'm a noob so any help or suggestions would be much appreciated. 我已经听过很多次使用正则表达式解析xml了,我是一个菜鸟,所以对您的帮助或建议将不胜感激。
Edit: 编辑:
Okay I've got something here but it seems I am building incomplete sentence along with complete sentences. 好的,我这里有东西,但似乎我正在构建不完整的句子以及完整的句子。
String h = new String();
List<String> sent = new ArrayList<>();
Document doc = getDocumentXML(xml);
doc.normalize();
System.out.println("Root node: " + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("Template");
for (int tmp = 0; tmp < nList.getLength(); tmp++) {
Node nNode = nList.item(tmp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
NodeList nl = nNode.getChildNodes();
for(int j=0; j<nl.getLength(); j++) {
Node node = nl.item(j);
if(nl.item(j).getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element) node;
if( e.hasAttribute("id") ) {
String nameNode = e.getNodeName();
System.out.println("GetNodeName: "+nameNode);
Integer currentAttrNum = Integer.parseInt( e.getAttribute("id") );
h += e.getTextContent();
System.out.println("Current id num: "+currentAttrNum);
if(e.getNodeType() == Node.ELEMENT_NODE && !e.getNextSibling().hasAttributes()) {
System.out.println("last sibling");
sent.add( h );
}
}
}
}
for(String s : sent) {
System.out.println("Sentence: "+s);
}
}
}
I get the following output in my foreach loop: 我在foreach循环中得到以下输出:
Sentence: You don't qualify because
Sentence: You don't qualify because ApplicableConditions
Sentence: You don't qualify because ApplicableConditions.
Sentence: You don't qualify because ApplicableConditions.Your eligibility for this credit is not affected since
Sentence: You don't qualify because ApplicableConditions.Your eligibility for this credit is not affected since ApplicableConditions
Sentence: You don't qualify because ApplicableConditions.Your eligibility for this credit is not affected since ApplicableConditions.
I should only have: 我应该只有:
Sentence: You don't qualify because ApplicableConditions.
Sentence: Your eligibility for this credit is not affected since ApplicableConditions.
Can you find the bug in my code? 您可以在我的代码中找到该错误吗?
I don't know much about XML (and by much I mean nothing at all) but I'll try to help. 我对XML不太了解(而且我一点也不表示什么),但我会尽力提供帮助。 If you get a text output you can return
in Java, you can take that text and do something along the lines of 如果获得文本输出,则可以用Java return
,可以使用该文本并按照以下步骤进行操作
/*regexNameHere is the name you give the array, inputTextVar is the variable
*(make sure it's a string!) assigned to the text you receive from the XML process
*/
String [] (regexNameHere) = (inputTextVar).split("character to split by");
//This is what you use to declare variables...
String var1 = regexNameHere[0];
String var2 = regexNameHere[1];
And so on. 等等。 If the variable regexNameHere
was equal to the string "Regex split string" and the .split
argument is (" ")
(a space) then regexNameHere[0]
would equal "Regex", the regexNameHere[1]
would be "split" and regexNameHere[2]
would be "string". 如果变量regexNameHere
等于字符串“ Regex split string”,并且.split
参数为(" ")
(空格),则regexNameHere[0]
等于“ Regex”, regexNameHere[1]
将为“ split”,并且regexNameHere[2]
将为“字符串”。
If you want to split something like the "ApplicableConditions" in your text, I would imagine you just put "Applicable" as the .split
argument, and regexNameHere[0]
would equal "Applicable" and regexNameHere[1]
would be equal to "Conditions." 如果您想在文本中拆分“ ApplicableConditions”之类的内容,我想您只是将“ Applicable”用作.split
参数,而regexNameHere[0]
等于“ Applicable”,而regexNameHere[1]
等于“条件。”
Hope this helped, and good luck! 希望这会有所帮助,并祝你好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.