0

I am receiving a xml in string format. Is there any library to search for elements in the string?

 <Version value="0"/>
    <IssueDate>2017-12-15</IssueDate>
    <Locale>en_US</Locale>
    <RecipientAddress>
        <Category>Primary</Category>
        <SubCategory>0</SubCategory>
        <Name>Vitsi</Name>
        <Attention>Stowell Group Llc.</Attention>
        <AddressLine1>511 6th St</AddressLine1>
        <City>Lake Oswego</City>
        <Country>United States</Country>
        <PresentationValue>Lake Oswego OR 97034-2903</PresentationValue>
        <State>OR</State>
        <ZIPCode>97034</ZIPCode>
        <ZIP4>2903</ZIP4>
    </RecipientAddress>
    <RecipientAddress>
        <Category>Additional</Category>
        <SubCategory>1</SubCategory>
        <Name>Vitsi</Name>
        <AddressLine1>Po Box 957</AddressLine1>
        <City>Lake Oswego</City>
        <Country>United States</Country>
        <PresentationValue>Lake Oswego OR 97034-0104</PresentationValue>
        <State>OR</State>
        <ZIPCode>97034</ZIPCode>
        <ZIP4>0104</ZIP4>
    </RecipientAddress>
    <SenderName>TMO</SenderName>
    <SenderId>IL</SenderId>
    <SenderAddress>
        <Name>T-mobile</Name>
        <AddressLine1>Po Box 790047</AddressLine1>
        <City>St. Louis</City>
        <PresentationValue>ST. LOUIS MO 63179-0047</PresentationValue>
        <State>MO</State>
        <ZIPCode>63179</ZIPCode>
.
.
.
.

I want to access the element RecipientAddress, which is a list. Is there any library to do that? Please note that what I receive is a string. It is an invoice and there will be many to process, so performance is important

3 Answers 3

1

Following options are available:

  1. Convert xml string to java objects using JAXB.
  2. Use .indexOf() in string method to retrieve specific parts of xml.
  3. Use regular expression to retrieve specific parts of xml.
  4. SAX/DOM/STAX parser for parsing and extraction from xml.
  5. Xpath for fetching the specific values from xml.
Sign up to request clarification or add additional context in comments.

Comments

0

You could use XPATH. Java has inbuilt support for XML querying without any thirdparty library,

Code piece would be,

String xmlInputStr = "<YOUR_XML_STRING_INPUT>"

String xpathExpressionStr = "<XPATH_EXPRESSION_STRING>"

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(xmlInputStr);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile(xpathExpressionStr);

You can write your own expression string for querying. Typical example

"/RecipientAddress/Category"

Evaluate your xml against expression to retrieve list of nodes.

NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

And iterate over nodes,

for (int i = 0; i < nodes.getLength(); i++) {
   Node nNode = nodes.item(i);
   ...
}

Comments

0

There lot of pre-implemented api is available to convert xml to java object.

please look at that the xerces from Apache.

If you want extract only specified value the put whole in to string and use indexOf("string")

1 Comment

Thanks. I dont have an xml object, I have a String object. Also if I have to convert the object to xml my worry is that the performance would be affected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.