繁体   English   中英

如何从XML响应Java解析文本并将其组合在一起

[英]How to parse and put together pieces of text from an XML response Java

我有以下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>

我希望能够编写一些Java类,以便在传递xpathscenario时能够组合/构造Template节点下的文本节点(这样我们就知道要使用哪个Template)。

例:

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

    return constructedSentence;
}

输出:

您没有资格,因为有适用条件。

等等...

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

编辑:

好的,我这里有东西,但似乎我正在构建不完整的句子以及完整的句子。

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循环中得到以下输出:

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.

您可以在我的代码中找到该错误吗?

我对XML不太了解(而且我一点也不表示什么),但我会尽力提供帮助。 如果获得文本输出,则可以用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];

等等。 如果变量regexNameHere等于字符串“ Regex split string”,并且.split参数为(" ") (空格),则regexNameHere[0]等于“ Regex”, regexNameHere[1]将为“ split”,并且regexNameHere[2]将为“字符串”。

如果您想在文本中拆分“ ApplicableConditions”之类的内容,我想您只是将“ Applicable”用作.split参数,而regexNameHere[0]等于“ Applicable”,而regexNameHere[1]等于“条件。”

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM