searching string and appending - java

I have below xml in string object
<Emp>
<e1>
<x>ABC</x>
</e1>
<e2>
<x>XYZ</x>
</e2>
<e3>
<x>TTT</x>
</e3>
</Emp>
if i search string with "ABC" then i want to add test
so xml became
<Emp>
<e1>
<x>ABC</x>
<fail>test</fail>
</e1>
<e2>
<x>XYZ</x>
</e2>
<e3>
<x>TTT</x>
</e3>
</Emp>
if i search string with "XYZ" then i want to add test in e2
<Emp>
<e1>
<x>ABC</x>
</e1>
<e2>
<x>XYZ</x>
<fail>test</fail>
</e2>
<e3>
<x>TTT</x>
</e3>
</Emp>
Please let me know whow to use sub string here.

Related

Java check username and password for matching(from xml file)

#XmlElement(name = "Emp", type = UserBean.class)
#XmlElementWrapper(name = "Emps")
private List<UserBean> users;
if (users.stream().anyMatch(x -> x.getUsername().equals(userBean.getUsername()))
&& users.stream().anyMatch(x -> x.getPassowrd().equals(userBean.getPassowrd()))) {
login=true;
}
This function checks whether a password and name exist in a container. I fill the container from xml file. However, that only check that items are existing, not the matching.
xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Department>
<Emps>
<Emp>
<username>Name</username>
<password>name</password>
</Emp>
<Emp>
<username>Name2</username>
<password>name2</password>
</Emp>
</Emps>
</Department>
So I can enter username Name and the password name2 and login still will be true. Are they any ways to set login to true only if username and password are matching?
Just merge the conditions into a single predicate:
if (users.stream().anyMatch(x -> x.getUsername().equals(userBean.getUsername())
&& x.getPassowrd().equals(userBean.getPassowrd())))

Trying to write quotes (") in xml using java

I have a long string which contains ~" & "~ etc.
but when I am trying to write it in xml the o/p is: ~&quot
please suggest a way to write the complete string including "(double quotes)
Below are my code:
for(String str:Parser.queryList){
Element query = doc.createElement("query");
view.appendChild(screen);
screen.appendChild(query);
query.setAttribute("query", str);
}
output: OP =~"=~"

XStream apostrophe Issue in converting Java Object to XML

I am using com.thoughtworks.xstream.XStream to Generate xml String. I parse Object to xstream.toXML method and I get the xml output according to the way I need.
<myxml>
<test type="test" name="test">
<question id="Name" answer="Micheal"/>
<question id="Address" answer="Home">
<details name="First Address">
<detailanswer>friend&apos;s House</detailanswer>
</details>
</basequestion>
</test>
</myxml>
XStream xstream = new XStream();
xstream.alias("myxml", MyXml.class);
xstream.alias("test", Test.class);
xstream.alias("question", Question.class);
xstream.alias("details", Details.class);
xstream.alias("detailanswer", String.class);
xstream.addImplicitCollection(MyXml.class, "test");
xstream.addImplicitCollection(Test.class, "question");
xstream.addImplicitCollection(Question.class, "details");
xstream.addImplicitCollection(Details.class, "detailanswer");
xstream.useAttributeFor(Test.class, "type");
xstream.useAttributeFor(Test.class, "name");
xstream.useAttributeFor(Question.class, "id");
xstream.useAttributeFor(Question.class, "answer");
xstream.useAttributeFor(Details.class, "name");
return xstream.toXML(eform);
Following is the Object structure.
Inside MyXml there is List<Test>
Test has List<Question>, String type, String name
Question has List<Details>, String id, String answer.
Details has List<String> detailAnswer, String name
So the element in the question, Friend's house is added to the List detailAnswer in Details class.
I get friend&apos;s House instead of friend's house. How can I resolve this. Is there special way to convert using XStream?
I think its better to use java method to replace a character.
xStream.toXML(testPojo).replaceAll("&apos;", "'")

JSONException: no value for XYZ when trying to getString("XYZ")

I am doing JSON parsing in Android by the following steps:
Get an XML response from a web-service using HttpPost object.
Convert this XML to JSON string then JSON object.
Now the problem is that sometimes the XML response has null string or Null tag.
For Example:
<data>
<name>Martin Clark</name>
<city>London</city>
<country>XYZ</country> or <country /> <!-- Sometimes it will blank string like this if country is not available -->
<age>27</age>
</data>
Parsing style:
jsonObject.getString("country"); // It is working perfect when xml is this : <country>XYZ<country/>
jsonObject.getString("country"); // It is giving Exception key is not found when xml is this : <country />
i don't understand why the parser is not giving me BLANK string for blank XML object.
By deep level debugging i have found that XML to JSON converter not produce object corresponding to blank xml object.
Please help me.
Use optString instead, catching the Exception is costly and unnecessary.
public String optString (String name)
Added in API level 1 Returns the value mapped by name if it exists,
coercing it if necessary. Returns the empty string if no such mapping
exists.
public String optString (String name, String fallback)
Added in API level 1 Returns the value mapped by name if it exists,
coercing it if necessary. Returns fallback if no such mapping exists.
Documentation
You can use ths logical solution for your problem.
Try this once.
public static String getStringFromJSON(JSONObject json, String key){
String value = ""; // Blank string by default.
try {
String value = json.getString(key);
return value;
}
catch(JSONException exp){
exp.getMessage();
}
return value; // this wil return BLANk string if object is not prasent.
}
You can you this method for getting String from json object,

Parsing XML File with JAXB based on attribute values

I have a Maven and Spring based Java web application.
I have a config.xml file in src/main/resources.
<?xml version="1.0" encoding="UTF-8"?>
<sourceConfigs>
<areas>
<area name="Defects">
<fileHeaders>ID,Issue Key,Fields
</fileHeaders>
</area>
<area name="Organization">
<fileHeaders>ID,Org Key,Fields
</fileHeaders>
</area>
</areas>
<sourceTypes>
<source name="source1">
<adapterObject>source1Adapter</adapterObject>
<resultObject>JsonObject</resultObject>
</source>
<source name="source2">
<adapterObject>source2Adapter</adapterObject>
<resultObject>sourceObject</resultObject>
</source>
</sourceTypes>
</sourceConfigs>
I want to parse the above XML to Java object based on the attributes.
I have created two classes.
Area.java
#XmlRootElement(name="area")
public class Area {
String name;
String fileHeaders;
#XmlAttribute
//get Name
//set Name
#XmlElement
//get fileHeaders
//set FileHeaders
}
Source.java
#XmlRootElement(name="source")
public class Source {
String name;
String adapterObject;
String resultObject;
#XmlAttribute
//get Name
//set Name
#XmlElement
//get adapterObject
//set adapterObject
#XmlElement
//get resultObject
//set resultObject
}
I want to parse the XML based on the attribute value.
ie; if the attribute value of area is Defects, the parsed object should have the values based on that, else if its Organization, then the values based on that and object type to Area object. Similarly for Source type also.
How can I do that?
When it was only a simple XML file like following
<?xml version="1.0" encoding="UTF-8"?>
<sourceConfig area="Defects">
<adapterObject>jAdapter</adapterObject>
<resultObject>jsonObject</resultObject>
</sourceConfig>
My POJO was based on that and the code I used to parse is
public SourceConfig getConfigObject() throws JAXBException, IOException {
JAXBContext jaxbContext = JAXBContext.newInstance(SourceConfig.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Resource resource=new ClassPathResource("config.xml");
File file=resource.getFile();
SourceConfig sourceConfig = (SourceConfig) jaxbUnmarshaller.unmarshal(file);
return sourceConfig;
}
But for this complex I don't know how to parse based on attribute values, and also multiple list of data.
How to parse based on the attribute values?
I have created two POJOs for parsing different kind. If it's a single class also it's fine.
UPDATE 1
My expected output.
When I pass the value "Defects" when unmarshalling the Area object, The values should be
name= Defects
fileHeaders=ID,Issue Key,Fields
And If I pass "Organization", The area object values should be based on that. Similarly when unmarshalling source.
Now I am getting as List of Areas and List of SourceTypes, then I take the corresponding object by checking the value.
May be is there any way to parse only selected one based on attribute value instead of getting list and then checking value and returning the object?

Categories