Trim all Strings in a Node (Java webservice) - java

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.

Related

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

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

Creating xml using DOM in java

I am trying to create a an XML with multiple elements. Below is the code i am trying.
Element root = doc.createElement("root");
doc.appendChild(root);
Element member = doc.createElement("member");
root.appendChild(member);
Element name = doc.createElement("name");
name.appendChild(doc.createTextNode("xxx"));
member.appendChild(name);
Element phone = doc.createElement("phone");
phone.appendChild(doc.createTextNode("vvvv"));
member.appendChild(phone);
Element sss = doc.createElement("somethingNew");
root.appendChild(sss);
Element nnn = doc.createElement("name1");
nnn.appendChild(doc.createTextNode("AAA"));
sss.appendChild(nnn);
Element ppp = doc.createElement("phoneEx");
ppp.appendChild(doc.createTextNode("cc"));
sss.appendChild(ppp);
And the output i am getting is
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<member>
<name>xxx</name>
<phone>vvvv</phone>
<name>xxx</name>
<phone>vvvv</phone>
</member>
<somethingNew/>
</root>
But i am expecting somthing like Below, Please help me where i am going wriong
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<member>
<name>xxx</name>
<phone>vvvv</phone>
<name>xxx</name>
<phone>vvvv</phone>
</member>
<somethingNew>
<name1>DDD</name1>
<phoneEx>CC</phoneEx>
</somethingNew>
</root>
Never Mind i could able to find the solution, I am appending to the first element all the time, That was the issue.Corrected my code

Java XPathExpression for Child Element Using Parent Element As XPath Parameter

For the following xml:
<books>
<book>
<author>Peter</author>
<title>Tales from Somewhere</title>
<data>
<version>1</version>
</data>
</book>
<book>
<author>Paul</author>
<title>Tales from Nowhere</title>
<data>
<version>2</version>
</data>
</book>
</books>
How can I get the <version> value of the book author 'Paul' above, using this type of notation for building a Java XPathExpression:
//*[local-name()='books']/*
?
I used the following question as a reference:
Get first child node in XSLT using local-name()
Thanks!
This XPath will get the version of a book where there is at an author element with the value "Paul":
//book[author="Paul"]/data/version
When run against this XML:
<books>
<book>
<author>Peter</author>
<title>Tales from Somewhere</title>
<data>
<version>1</version>
</data>
</book>
<book>
<author>Paul</author>
<title>Tales from Nowhere</title>
<data>
<version>2</version>
</data>
</book>
<book>
<author>Peter</author>
<author>Paul</author>
<title>How to write a book with a friend</title>
<data>
<version>7</version>
</data>
</book>
</books>
You get this result:
<version>1</version>
<version>7</version>

parsing links from given file using jsoup

I am using Jsoup to parse xml file stored in filesystem,But when I parse link element changes its scope...
XML file:-
<movies>
<movie>
<id>0</id>
<name>Aag - 1948</name>
<link>http://www.songspk.pk/indian/aag_1948.html</link>
</movie>
<movie>
<id>1</id>
<name></name>
<link>#</link>
</movie>
<movie>
<id>2</id>
<name>Aa Ab Laut Chalain</name>
<link>http://www.songspk.pk/aa_ab_laut_chalein.html</link>
</movie>
<movie>
<id>3</id>
<name>Aag - RGV Ki Aag</name>
<link>http://www.songspk.pk/aag.html</link>
</movie>
</movies>
Java implementation:-
public class DownloadSongsList {
private static Document document;
public static void main(String...string) throws IOException{
document = Jsoup.parse(new File("c:/movies.xml"), "UTF-8");
Elements movies = document.getElementsByTag("movies");
System.out.println(movies.html());
}
}
Output:-
<movie>
<id>
0
</id>
<name>
Aag - 1948
</name>
<link /> http://www.songspk.pk/indian/aag_1948.html
</movie>
<movie>
<id>
1
</id>
<name></name>
<link />#
</movie>
<movie>
<id>
2
</id>
<name>
Aa Ab Laut Chalain
</name>
<link />http://www.songspk.pk/aa_ab_laut_chalein.html
</movie>
<movie>
<id>
3
</id>
<name>
Aag - RGV Ki Aag
</name>
<link />http://www.songspk.pk/aag.html
</movie>
I want to parse links but can't get due to this problem.
And I would like to stick to Jsoup because I use this same library to create the following xml files...
Have you tried using the Parser.xmlParser()?
Example:
Document doc = Jsoup.parse(new File("c:/movies.xml"), "", Parser.xmlParser());
Elements movies = doc.getElementsByTag("movies");
System.out.println(movies.html());
Should output:
<movie>
<id>
0
</id>
<name>
Aag - 1948
</name>
<link>
http://www.songspk.pk/indian/aag_1948.html
</link>
</movie>
<movie>
<id>
1
</id>
<name></name>
<link>
#
</link>
</movie>
<movie>
<id>
2
</id>
<name>
Aa Ab Laut Chalain
</name>
<link>
http://www.songspk.pk/aa_ab_laut_chalein.html
</link>
</movie>
<movie>
<id>
3
</id>
<name>
Aag - RGV Ki Aag
</name>
<link>
http://www.songspk.pk/aag.html
</link>
</movie>
So then you can extract the <link> tags normally:
Elements links = doc.getElementsByTag("link");

Merging of xml files java

I have two different xml files described as below and want to merge these xml files and get the expected output may be using xpath or dom parsing but not XSLT since the xmls are always not the same
XML1.xml
<personinfo>
<person>
<name><name>
<age></age>
<address>
<street></street>
<city></city>
<address>
</person>
<person>
<name><name>
<age></age>
<address>
<street></street>
<city></city>
<address>
</person>
<person>
<name><name>
<age></age>
<address>
<street></street>
<city></city>
<address>
</person>
</personinfo>
XML2.xml
<personinfo>
<person>
<name>tom<name>
<age>26</age>
<address>
<street>main street</street>
<city>washington</city>
<address>
</person>
<person>
<name>mike<name>
<age>30</age>
<address>
<street>first street</street>
<city>dallas</city>
<address>
</person>
</personinfo>
Expected.xml
<personinfo>
<person>
<name>tom<name>
<age>26</age>
<address>
<street>main street</street>
<city>washington</city>
<address>
</person>
<person>
<name>mike<name>
<age>30</age>
<address>
<street>first street</street>
<city>dallas</city>
<address>
</person>
<person>
<name><name>
<age></age>
<address>
<street></street>
<city></city>
<address>
</person>
</personinfo>
Thanks in advance ....
If you have the flexibility to create a new xml file, you can parse each of them using any parser you are comfortable with. Store the tags in a LinkedList of String LinkedLists and the tag values in a HashMap of the following type:
LinkedHashMap data= new LinkedHashMap();
You can then call the tag names from the linked lists, append the tag values from the Hash Map and write them out to a new XML file.
When I did merging of XMLs, this was the procedure I used.
Hope this helps
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
NodeList nodeLst = doc.getElementsByTagName("employee");
for (int s = 0; s < nodeLst.getLength(); s++)
{
stkey=getXMLData(s,nodeLst,"id");
keylist.add(stkey);// adding integer keys to a Linked List
data.put(stkey, stkey);
data.put(stkey+"first",getXMLData(s,nodeLst,"firstname"));
data.put(stkey+"last",getXMLData(s,nodeLst,"lastname"));
data.put(stkey+"loc",getXMLData(s,nodeLst,"location"));
data.put(stkey+"occ",getXMLData(s,nodeLst,"occupation"));
}
this will get the tag values in the hash map and the tag names in the linked list. to make your work easier, you can append the type of tag to the hashmap key. For example: if my key is the Employee ID(in my case), I append "first" to it. Lets say some one has an id: 10001. his data would be stored as: 10001, then 10001first, 10001last, 10001loc,10001occ. Now, you can call each hashmap key, get the element as per appended tag name and concatenate to your xml file.
Hope this helps.

Categories