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