如何从 XML 响应 Java 解析和组合文本片段 [英] How to parse and put together pieces of text from an XML response Java

查看:29
本文介绍了如何从 XML 响应 Java 解析和组合文本片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XML 响应:

I have the following XML response:

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

当我传入 xpath 时,我希望能够编写一些 Java 类,以便能够在 Template 节点下组合/构造文本节点和 scenario(这样我们就知道要使用哪个模板).

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

示例:

public String constructSentence(String xpath, String scenario) {
    // some processing here

    return constructedSentence;
}

输出:

您不符合条件,因为 ApplicableConditions.

You don't qualify because ApplicableConditions.

等等...

如何使用 Java 完成此操作?最好的方法是什么?有什么建议吗?我听过很多次使用正则表达式来解析 xml 是一种罪过,我是一个菜鸟,所以任何帮助或建议将不胜感激.

How can I accomplish this using 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.

好的,我这里有一些东西,但似乎我正在构建不完整的句子和完整的句子.

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

我在 foreach 循环中得到以下输出:

I get the following output in my foreach loop:

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.

我应该只有:

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?

推荐答案

我对 XML 知之甚少(我的意思是根本没有),但我会尽力提供帮助.如果你得到一个文本输出,你可以在 Java 中 return,你可以获取该文本并按照

I don't know much about XML (and by much I mean nothing at all) but I'll try to help. If you get a text output you can return in Java, you can take that text and do something along the lines of

/*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];

等等.如果变量 regexNameHere 等于字符串Regex split string"并且 .split 参数是 (" ")(一个空格),那么regexNameHere[0] 将等于Regex",regexNameHere[1] 将是split",而 regexNameHere[2] 将是string".

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

如果您想在文本中拆分诸如ApplicableConditions"之类的内容,我想您只需将Applicable"作为 .split 参数和 regexNameHere[0] 等于Applicable",regexNameHere[1] 等于Conditions".

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

希望这有帮助,祝你好运!

Hope this helped, and good luck!

这篇关于如何从 XML 响应 Java 解析和组合文本片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆