How to capture certain text within a StringBuffer Java [duplicate] - java

This question already has answers here:
Java regex to extract text between tags
(8 answers)
Closed 4 years ago.
I have a StringBuffer object with the following contents:
<ET>read input: 1.629ms</ET>
<ET>There were 3 errors:
<Error>
<ErrorId>AllConditionsTrue</ErrorId>
<MetaData>
<Entry>
<Key>Balance Due</Key>
<Value>1500.99</Value>
</Entry>
</MetaData>
</Error>
<Error>
<ErrorId>Opposite</ErrorId>
<MetaData>
<Entry>
<Key>Node</Key>
</Entry>
</MetaData>
</Error>
<Error>
<ErrorId>minInclusive</ErrorId>
<MetaData>
<Entry>
<Key>Description</Key>
<Value>Wages Amount</Value>
</Entry>
</MetaData>
</Error>
: 0.027ms</ET>
<ET>convert: 319.414ms</ET>
<FORM id="123"/>
<DATA size="11920"/>
<ERROR code="0"/>
How can I capture just the text which is at and within the Error Tags (<Error> some text </Error> ). So my new String or StringBuffer object contains:
<Error>
<ErrorId>AllConditionsTrue</ErrorId>
<MetaData>
<Entry>
<Key>Balance Due</Key>
<Value>1500.99</Value>
</Entry>
</MetaData>
</Error>
<Error>
<ErrorId>Opposite</ErrorId>
<MetaData>
<Entry>
<Key>Node</Key>
</Entry>
</MetaData>
</Error>
<Error>
<ErrorId>minInclusive</ErrorId>
<MetaData>
<Entry>
<Key>Description</Key>
<Value>Wages Amount</Value>
</Entry>
</MetaData>
</Error>
How can I accomplish my goal using Java?
Edit
Trying both your guys solutions:
Pattern p = Pattern.compile("<Error>.*?<\\/Error>", Pattern.DOTALL);
Matcher m = p.matcher(buf.toString());
String errorText = "";
while (m.find()) {
errorText = m.group(1);
}
I seem to only get 3 error tag element not all 3.
Example:
<Error>
<ErrorId>minInclusive</ErrorId>
<MetaData>
<Entry>
<Key>Description</Key>
<Value>Wages Amount</Value>
</Entry>
</MetaData>
</Error>

Regex:
<Error>.*?<\/Error>
Demo

SaxParse would be a better solution than string parser.
It will be portable for your future references as well.
Refer this sax documentation :
http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/SAXParser.html

Note that your string contains new lines, so you have to use \n. Try this out:
<Error>((?:.*?\n?)+.*?)<\/Error>
Check the Regex101

Related

How to send request through SOAP UI with CDATA within CDATA to Java Application?

I am getting following exception while parsing XML
org.xml.sax.SAXParseException: The processing instruction target matching "[xX][mM][lL]" is not allowed.
SOAp Request i am sending:
<soap:Envelope>
<soap:Header/>
<soap:Body>
<ver:ReceiveOnlineBatchExternalAttachment>
<web:username>user</web:username>
<web:passwd>pass</web:passwd>
<web:receiversReference>1232323</web:receiversReference>
<web:sequenceNumber>1</web:sequenceNumber>
<web:batch>
<![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
<DataBatch>
<DataUnits>
<DataUnit>
<FormTask>
<ServiceCode>323015</ServiceCode>
<Form>
<FormData>
<![CDATA[<melding> </melding>]]
</FormData>
</Form>
</FormTask>
</DataUnit>
</DataUnits>
</DataBatch>
]]>>
</web:batch>
</ver:ReceiveOnlineBatchExternalAttachment>
</soap:Body>
</soap:Envelope>
I have done changes to CDATA multiple times but getting same error.
Could anyone suggest how CDATA within CDATA can be handled in this request?
Tried following but failed:
<![CDATA[ <elements> <![CDATA[<melding> </melding>]] <elements> ]]>>
<![CDATA[ <elements> <![CDATA[<melding> </melding>]]> <elements> ]]>
<![CDATA[ <elements> <![CDATA[<melding> </melding>]]> <elements> ]]>
I speculate that your problem is actually being caused by nested CDATA content. Can you try the following:
<![CDATA[
<DataBatch>
<DataUnits>
<DataUnit>
<FormTask>
<ServiceCode>323015</ServiceCode>
<Form>
<FormData>
<![CDATA[
<melding> </melding>
]]]]><![CDATA[>
</FormData>
</Form>
</FormTask>
</DataUnit>
</DataUnits>
</DataBatch>
]]>
The gist of the trick here is that when the parser hits the inner nested <![CDATA[ it will actually ignore it. Then, when it hits this:
]]]]><![CDATA[>
the first ]] will also be ignored, the following ]]> will be consumed, closing the outer CDATA, and then a new CDATA is immediately opened again which should last until the outer closing for that.
Edit by #fatherazrael:
Remove the XML tags <?xml version="1.0" encoding="UTF-8"?>
Reference: Nested CDATA - correctly

How to create entries with image element in the RSS Feed using the java ROME API?

I am trying to create the RSS Feeds using java ROME API. My requirement is that every entry should contain an Image as given below:
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
<channel>
<title>Sample RSS Build Results</title>
<link>http://time.is</link>
<description>sample RSS build</description>
<item>
<title>Ist Feed</title>
<link>http://mysampleurl1.com</link>
<description>The build was successful!</description>
<pubDate>Mon, 08 Aug 2016 10:28:32 GMT</pubDate>
<image>http://myimageurl1.com</image>
<dc:date>2016-08-08T10:28:32Z</dc:date>
</item>
<item>
<title>IInd Feed</title>
<link>http://mysampleurl2.com</link>
<description>The build was successful!</description>
<pubDate>Mon, 08 Aug 2016 10:28:44 GMT</pubDate>
<dc:date>2016-08-08T10:28:44Z</dc:date>
</item>
</channel>
I am new to java ROME api. It provides the package :: com.rometools.rome.feed.synd.SyndImageImpl to set/get image item in complete feed but not in individual entries. For an entry in RSS feed it has package :: com.rometools.rome.feed.synd.SyndEntryImpl but it does not provide any function for setting or getting image.
Please help me to resolve this issue.
Thanks in advance.
The RSS spec doesn't specify image elements for entries, but you can extend it with Image namespace.
Short solution could be like this:
SyndEntry entry = new SyndEntryImpl();
..
Element image = new Element("image", Namespace.getNamespace("image", "http://web.resource.org/rss/1.0/modules/image/"));
image.addContent("http://localhost/feed/item1_image");
entry.getForeignMarkup().add(image);
This will result in valid xml:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>title</title>
<link>http://localhost/feed</link>
<description>description</description>
<item>
<title>entry title 1</title>
<link>http://localhost/feed/item1</link>
<image:image xmlns:image="http://web.resource.org/rss/1.0/modules/image/">http://localhost/feed/item1_image</image:image>
<guid isPermaLink="false">http://localhost/feed/item1</guid>
</item>
</channel>
</rss>
More robust way is to create a custom module like they've done here and here.

Generate source files from XML

I have the XML file which starts like this:
<?xml version="1.0" encoding="UTF-8"?>
<interface name="AccountAPING" owner="BDP" version="1.0.0" date="now()" namespace="com.betfair.account.api"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<description>Account API-NG</description>
...
afterward there are various blocks, such as:
<operation name="getDeveloperAppKeys" since="1.0.0">
<description>
Get all application keys owned by the given developer/vendor
</description>
<parameters>
<request/>
<simpleResponse type="list(DeveloperApp)">
<description>
A list of application keys owned by the given developer/vendor
</description>
</simpleResponse>
<exceptions>
<exception type="AccountAPINGException">
<description>Generic exception that is thrown if this operation fails for any reason.</description>
</exception>
</exceptions>
</parameters>
</operation>
........
<simpleType name="Status" type="string">
<validValues>
<value name="SUCCESS">
<description>Sucess status</description>
</value>
</validValues>
</simpleType>
........
<dataType name="TimeRange">
<description>TimeRange</description>
<parameter name="from" type="dateTime" mandatory="false">
<description>from, format: ISO 8601)</description>
</parameter>
<parameter name="to" type="dateTime" mandatory="false">
<description>to, format: ISO 8601</description>
</parameter>
</dataType>
How can I generate Java code from this using maven? I tried using "maven-jaxb2-plugin", but it can't parse this structure.
Please note
This is an XML file not not an xsd
I'm using Netbeans
First of all, you need the schema (xsd) that describes your xml sample. Without that schema you can not use Jaxb. You don't have a schema for the sample you shown xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" is not the schema for your xml.
You can use free on-line tools to generate schemas from xml, but you can't rely on this tools without review the automated schema.
To generate Java code from a schema file use XJC, see here. Open a command prompt to the folder where you put your xsd file, and then generate java code you'll just need to type:
$ xjc nameOfSchemaFile.xsd
xjc is included with Java SDK.

Trim all Strings in a Node (Java webservice)

I am getting the XML data that is sent as part of SOAP request using a handler in webservice before unmarshalling of the request is done.
Node root = soapMsgContext.getMessage().getSOAPBody().getFirstChild();
Is there to trim all the elements in the node? For instance in the below case -
<Person>
<Name> J i m </Name>
<Details>
<age> 1 9 </age>
<Address>
<firstName> J i mm y </firstName>
<lastName> An der son </lastName>
</Address>
</Details>
<Person>
must be changed to
<Person>
<Name>Jim</Name>
<Details>
<age>19</age>
<Address>
<firstName>Jimmy</firstName>
<lastName>Anderson</lastName>
</Address>
</Details>
<Person>
EDIT: Webservices framework being used is JAX-RPC.

406 Not acceptable while performing post on ODATA4j

Am a newbie in odata4j concepts.
Using odata4j Library odata4j-0.5-nojpabundle.jar launched producer on tomcat using link
http://code.google.com/p/odata4j/wiki/Tomcat.
My producer is modified to give an large list of integers as an entity-set called "Integers"
I could retreive the serviceDoc,collection and can apply filters.
Now trying to perform post on this service doc[trying to add one more entry with same schema ].
Doing post for http://localhost:8080/OData/example.svc/Integers
with post body :
<?xml version="1.0" encoding="utf-8" ?> <edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"> <entry> <id>http://localhost:8080/OData/example.svc/Integers(100)</id>
<title type="text" />
<updated>2011-12-29T10:50:33Z</updated>
<author> <name /> </author>
<link rel="edit" title="Integers" href="Integers(100)" />
<category term="example.Integers" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:EntityId m:type="Edm.Int32">100</d:EntityId> </m:properties>
</content>
</entry>
am getting 406 not Acceptable Unknown content type application/xml;charset=UTF-8.
Int collection content type is application/xml. Still not able to find out why is this response is obtained.
Does anyone know what i am missing here.
Thanks in Advance.
Use application/atom+xml as the Content-Type
Entry should be the document element (your xml above is not well-formed)
See: http://www.odata.org/developers/protocols/operations#CreatingnewEntries
Hope that helps,
- john

Categories