Rome API does not parse the image URL if the URL is given within the CDATA section.
For example, http://www.espn.com/espn/rss/espnu/news this feed has
<image>
<![CDATA[
URL of the image
]]>
</image>
Within the SyndFeed resulting from SyndFeedInput, I have checked the foreignMarkups, enclosures, DCModules.
value of other elements, such as Description and Title are also given within the CDATA, and Rome API is able to parse these values.
code snippet
XmlReader xmlReader = null;
try {
xmlReader = new XmlReader(new URL("http://www.espn.com/espn/rss/espnu/news"));
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(xmlReader);
} catch (Exception e) {
e.printStackTrace();
}
I looked into the API in more details. The API provides plugins to override the parsing
https://rometools.github.io/rome/RssAndAtOMUtilitiEsROMEV0.5AndAboveTutorialsAndArticles/RssAndAtOMUtilitiEsROMEPluginsMechanism.html
I wrote a class that extends RSS20Parser implements WireFeedParser and override the parseItem method
#Override
public Item parseItem(Element rssRoot, Element eItem, Locale locale) {
Item item = super.parseItem(rssRoot, eItem, locale);
Element imageElement = eItem.getChild("image", getRSSNamespace());
if (imageElement != null) {
String imageUrl = imageElement.getText();
Element urlElement = imageElement.getChild("url");
if(urlElement != null)
{
imageUrl = urlElement.getText();
}
Enclosure e = new Enclosure();
e.setType("image");
e.setUrl(imageUrl);
item.getEnclosures().add(e);
}
return item;
}
Now in SyndFeed, access the enclosures list and you will be able to find the image URL
List<SyndEntry> entries = feed.getEntries();
for (SyndEntry entry : entries) {
...
...
List<SyndEnclosure> enclosures = entry.getEnclosures();
if(enclosures!=null) {
for(SyndEnclosure enclosure : enclosures) {
if(enclosure.getType()!=null && enclosure.getType().equals("image")){
System.out.println("image URL : "+enclosure.getUrl());
}
}
}
}
and create a rome.properties file which is accessible in classpath with following entry
WireFeedParser.classes=your.package.name.CustomRomeRssParser
Related
I'm trying to create my own stream parser using the Kinesis Video Stream library. I'd like to parse the fragments and then write an .mkv to disk. Per the example, I'm trying to use the FragmentMetadataVisitor to get the next fragment number to do a subsequent GetMediaRequest. Debugging, it looks like none of the MkvElements contain "TAG" metadata. Also, looking at the source code, it looks like FragmentMetadataVistior ignores the AWS tags:
public void visit(MkvDataElement dataElement) throws MkvElementVisitException {
if (mkvTagProcessor.isPresent()) {
if (MkvTypeInfos.TAGNAME.equals(dataElement.getElementMetaData().getTypeInfo())) {
tagName = getMkvElementStringVal(dataElement);
} else if (MkvTypeInfos.TAGSTRING.equals(dataElement.getElementMetaData().getTypeInfo())) {
tagValue = getMkvElementStringVal(dataElement);
}
if (tagName != null && tagValue != null) {
// Only process non-internal tags
if (!tagName.startsWith(AWS_KINESISVIDEO_TAGNAME_PREFIX)) {
mkvTagProcessor.get().process(new MkvTag(tagName, tagValue), currentFragmentMetadata);
}
// Empty the values for new tag
tagName = null;
tagValue = null;
}
}
}
}
I'm using the BasicMkvTagProcessor to process the tags. However, debugging, the process(tag, metadata) method never gets called. I'm using the FragmentMetadataVisitor like so:
StreamingMkvReader streamingMkvReader =
StreamingMkvReader.createDefault(new InputStreamParserByteSource(result.getPayload()));
try(WritableByteChannel channel = FileChannel.open(generatePath(), StandardOpenOption.APPEND,StandardOpenOption.CREATE)){
while(streamingMkvReader.mightHaveNext()){
Optional<MkvElement> nextItem = streamingMkvReader.nextIfAvailable();
if(nextItem.isEmpty()){
return Optional.empty();
}
MkvElement item = nextItem.get();
/*Write to file*/
} catch(IOException e){
throw new RuntimeException("Failed to create file.", e);
} catch(MkvElementVisitException e){
throw new RuntimeException("An error occurred while writing MkvElement to file.");
}
return /*new StartSelector for next fragment using fragmentMetadataVisitor.getCurrentFragmentMetadata() to find the last fragment number*/;
}
Given this, is there another class I'm supposed to use to pull the AWS metadata described in the GetMedia documentation? Or am I misunderstanding?
AWS just added the GetClip API which I can use instead.
https://docs.aws.amazon.com/kinesisvideostreams/latest/dg/API_reader_GetClip.html
I am looking for a way how to get app versionCode and VersionName from google play with package name via java app in PC.
I have seen: https://androidquery.appspot.com/ but it not working anymore and also https://code.google.com/archive/p/android-market-api/ started to making problems and also stopped working, and it requer device ID.
Can you help me with some simple solution or API for this?
Very important, i need versionCode and VersionName and VersionName is relatively easy to get by parsing html google play app site. The versionCode is very important.
There is no official Google Play API, Playstore uses an internal protobuf API which is not documented and not open. IMHO, you could :
use an open source library that reverse engineer the API
scrap apk download sites that have already extracted this information (most likely via the same protobuf Google Play API)
Note that there is a Google Play developer API but you can't list your apks, versions, apps. It's essentially used to manage the app distribution, reviews, edits etc..
Google play internal API
play-store-api Java library
This library uses Google Play Store protobuf API (undocumented and closed API) and requires an email/password to generate a token that can be reused to play with the API :
GplaySearch googlePlayInstance = new GplaySearch();
DetailsResponse response = googlePlayInstance.getDetailResponse("user#gmail.com",
"password", "com.facebook.katana");
AppDetails appDetails = response.getDocV2().getDetails().getAppDetails();
System.out.println("version name : " + appDetails.getVersionString());
System.out.println("version code : " + appDetails.getVersionCode());
with this method :
public DetailsResponse getDetailResponse(String email,
String password,
String packageName) throws IOException, ApiBuilderException {
// A device definition is required to log in
// See resources for a list of available devices
Properties properties = new Properties();
try {
properties.load(getClass().getClassLoader().getSystemResourceAsStream("device-honami" +
".properties"));
} catch (IOException e) {
System.out.println("device-honami.properties not found");
return null;
}
PropertiesDeviceInfoProvider deviceInfoProvider = new PropertiesDeviceInfoProvider();
deviceInfoProvider.setProperties(properties);
deviceInfoProvider.setLocaleString(Locale.ENGLISH.toString());
// Provide valid google account info
PlayStoreApiBuilder builder = new PlayStoreApiBuilder()
.setDeviceInfoProvider(deviceInfoProvider)
.setHttpClient(new OkHttpClientAdapter())
.setEmail(email)
.setPassword(password);
GooglePlayAPI api = builder.build();
// We are logged in now
// Save and reuse the generated auth token and gsf id,
// unless you want to get banned for frequent relogins
api.getToken();
api.getGsfId();
// API wrapper instance is ready
return api.details(packageName);
}
device-honami.properties is device property file that is required to identify device characteristics. You have some device.properties file sample here
The OkHttpClientAdapter can be found here
Dependencies used to run this example :
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
compile 'com.github.yeriomin:play-store-api:0.19'
compile 'com.squareup.okhttp3:okhttp:3.8.1'
}
Scrap third part apk download sites
http://apk-dl.com
You could get the version name & version code from http://apk-dl.com (of course unofficial) by scraping the page with jsoup for the required package name :
String packageName = "com.facebook.katana";
Document doc = Jsoup.connect("http://apk-dl.com/" + packageName).get();
Elements data = doc.select(".file-list .mdl-menu__item");
if (data.size() > 0) {
System.out.println("full text : " + data.get(0).text());
Pattern pattern = Pattern.compile("(.*)\\s+\\((\\d+)\\)");
Matcher matcher = pattern.matcher(data.get(0).text());
if (matcher.find()) {
System.out.println("version name : " + matcher.group(1));
System.out.println("version code : " + matcher.group(2));
}
}
https://apkpure.com
Another possibility is scrapping https://apkpure.com :
String packageName = "com.facebook.katana";
Elements data = Jsoup.connect("https://apkpure.com/search?q=" + packageName)
.userAgent("Mozilla")
.get().select(".search-dl .search-title a");
if (data.size() > 0) {
Elements data2 = Jsoup.connect("https://apkpure.com" + data.attr("href"))
.userAgent("Mozilla")
.get().select(".faq_cat dd p");
if (data2.size() > 0) {
System.out.println(data2.get(0).text());
Pattern pattern = Pattern.compile("Version:\\s+(.*)\\s+\\((\\d+)\\)");
Matcher matcher = pattern.matcher(data2.get(0).text());
if (matcher.find()) {
System.out.println("version name : " + matcher.group(1));
System.out.println("version code : " + matcher.group(2));
}
}
}
https://api-apk.evozi.com
Also, https://api-apk.evozi.com has an internal JSON api but :
sometimes it doesn't work (return Ops, APK Downloader got access denied when trying to download) mostly for non popular app
it has mechanism in place against scraping bot (random token generated in JS with a random variable name)
The following is returning the version name and code with https://api-apk.evozi.com FWIW :
String packageName = "com.facebook.katana";
String data = Jsoup.connect("https://apps.evozi.com/apk-downloader")
.userAgent("Mozilla")
.execute().body();
String token = "";
String time = "";
Pattern varPattern = Pattern.compile("dedbadfbadc:\\s+(\\w+),");
Pattern timePattern = Pattern.compile("t:\\s+(\\w+),");
Matcher varMatch = varPattern.matcher(data);
Matcher timeMatch = timePattern.matcher(data);
if (varMatch.find()) {
Pattern tokenPattern = Pattern.compile("\\s*var\\s*" + varMatch.group(1) + "\\s*=\\s*'(.*)'.*");
Matcher tokenMatch = tokenPattern.matcher(data);
if (tokenMatch.find()) {
token = tokenMatch.group(1);
}
}
if (timeMatch.find()) {
time = timeMatch.group(1);
}
HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("https://api-apk.evozi.com/download");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("t", time));
params.add(new BasicNameValuePair("afedcfdcbdedcafe", packageName));
params.add(new BasicNameValuePair("dedbadfbadc", token));
params.add(new BasicNameValuePair("fetch", "false"));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = httpclient.execute(httppost);
JsonElement element = new JsonParser().parse(EntityUtils.toString(response.getEntity()));
JsonObject result = element.getAsJsonObject();
if (result.has("version") && result.has("version_code")) {
System.out.println("version name : " + result.get("version").getAsString());
System.out.println("version code : " + result.get("version_code").getAsInt());
} else {
System.out.println(result);
}
Implementation
You could implement it on a backend of yours that communicates directly with your Java application, this way you could maintain the process of retrieving version code/name if one of the above method fails.
If you are only interested in your own apps, a cleaner solution would be :
to set up a backend which will store all your current app version name / version code
all developer/publisher in your company could share a publish task (gradle task) which will use the Google Play developer API to publish apk and that gradle task would include a call to your backend to store the version code / version name entry when the app is published. The main goal would be to automate the whole publication with storage of the app metadata on your side.
Apart from using JSoup, we can alternatively do pattern matching for getting the app version from playStore.
To match the latest pattern from google playstore ie
<div class="BgcNfc">Current Version</div><span class="htlgb"><div><span class="htlgb">X.X.X</span></div>
we first have to match the above node sequence and then from above sequence get the version value. Below is the code snippet for same:
private String getAppVersion(String patternString, String inputString) {
try{
//Create a pattern
Pattern pattern = Pattern.compile(patternString);
if (null == pattern) {
return null;
}
//Match the pattern string in provided string
Matcher matcher = pattern.matcher(inputString);
if (null != matcher && matcher.find()) {
return matcher.group(1);
}
}catch (PatternSyntaxException ex) {
ex.printStackTrace();
}
return null;
}
private String getPlayStoreAppVersion(String appUrlString) {
final String currentVersion_PatternSeq = "<div[^>]*?>Current\\sVersion</div><span[^>]*?>(.*?)><div[^>]*?>(.*?)><span[^>]*?>(.*?)</span>";
final String appVersion_PatternSeq = "htlgb\">([^<]*)</s";
String playStoreAppVersion = null;
BufferedReader inReader = null;
URLConnection uc = null;
StringBuilder urlData = new StringBuilder();
final URL url = new URL(appUrlString);
uc = url.openConnection();
if(uc == null) {
return null;
}
uc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
inReader = new BufferedReader(new InputStreamReader(uc.getInputStream()));
if (null != inReader) {
String str = "";
while ((str = inReader.readLine()) != null) {
urlData.append(str);
}
}
// Get the current version pattern sequence
String versionString = getAppVersion (currentVersion_PatternSeq, urlData.toString());
if(null == versionString){
return null;
}else{
// get version from "htlgb">X.X.X</span>
playStoreAppVersion = getAppVersion (appVersion_PatternSeq, versionString);
}
return playStoreAppVersion;
}
I got this solved through this. Hope that helps.
Jsoup takes too long, its inefficient, for short easy way with pattermatching:
public class PlayStoreVersionChecker {
public String playStoreVersion = "0.0.0";
OkHttpClient client = new OkHttpClient();
private String execute(String url) throws IOException {
okhttp3.Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public String getPlayStoreVersion() {
try {
String html = execute("https://play.google.com/store/apps/details?id=" + APPIDHERE!!! + "&hl=en");
Pattern blockPattern = Pattern.compile("Current Version.*([0-9]+\\.[0-9]+\\.[0-9]+)</span>");
Matcher blockMatch = blockPattern.matcher(html);
if(blockMatch.find()) {
Pattern versionPattern = Pattern.compile("[0-9]+\\.[0-9]+\\.[0-9]+");
Matcher versionMatch = versionPattern.matcher(blockMatch.group(0));
if(versionMatch.find()) {
playStoreVersion = versionMatch.group(0);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return playStoreVersion;
}
}
public class Store {
private Document document;
private final static String baseURL = "https://play.google.com/store/apps/details?id=";
public static void main(String[] args) {
}
public Store(String packageName) {
try {
document = Jsoup.connect(baseURL + packageName).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0").get();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public String getTitle() {
return document.select("h1.AHFaub > span").text();
}
public String getDeveloper() {
return document.selectFirst("span.UAO9ie > a").text();
}
public String getCategory() {
Elements elements = document.select("span.UAO9ie > a");
for (Element element : elements) {
if (element.hasAttr("itemprop")) {
return element.text();
}
}
return null;
}
public String getIcon() {
return document.select("div.xSyT2c > img").attr("src");
}
public String getBigIcon() {
return document.select("div.xSyT2c > img").attr("srcset").replace(" 2x", "");
}
public List<String> getScreenshots() {
List<String> screenshots = new ArrayList<>();
Elements img = document.select("div.u3EI9e").select("button.Q4vdJd").select("img");
for (Element src : img) {
if (src.hasAttr("data-src")) {
screenshots.add(src.attr("data-src"));
} else {
screenshots.add(src.attr("src"));
}
}
return screenshots;
}
public List<String> getBigScreenshots() {
List<String> screenshots = new ArrayList<>();
Elements img = document.select("div.u3EI9e").select("button.Q4vdJd").select("img");
for (Element src : img) {
if (src.hasAttr("data-src")) {
screenshots.add(src.attr("data-srcset").replace(" 2x", ""));
} else {
screenshots.add(src.attr("srcset").replace(" 2x", ""));
}
}
return screenshots;
}
public String getDescription() {
return document.select("div.DWPxHb > span").text();
}
public String getRatings() {
return document.select("div.BHMmbe").text();
}
}
Imports
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
This script will return
Category (Personalization for example)
Developer Name
App Icon
App Name
Screenshots (Thumbnail and Full preview)
Description
You can also check the full source code here
I am getting one xml string, that I want to parse and get the data from it. I tried to parse it to json but I get the empty braces as a result.
public class ResultsActivity extends Activity {
String outputPath;
TextView tv;
public static int PRETTY_PRINT_INDENT_FACTOR = 4;
public static String TEST_XML_STRING;
DocumentBuilder builder;
InputStream is;
Document dom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
setContentView(tv);
String imageUrl = "unknown";
Bundle extras = getIntent().getExtras();
if( extras != null) {
imageUrl = extras.getString("IMAGE_PATH" );
outputPath = extras.getString( "RESULT_PATH" );
}
// Starting recognition process
new AsyncProcessTask(this).execute(imageUrl, outputPath);
}
public void updateResults(Boolean success) {
if (!success)
return;
try {
StringBuffer contents = new StringBuffer();
FileInputStream fis = openFileInput(outputPath);
try {
Reader reader = new InputStreamReader(fis, "UTF-8");
BufferedReader bufReader = new BufferedReader(reader);
String text = null;
while ((text = bufReader.readLine()) != null) {
contents.append(text).append(System.getProperty("line.separator"));
}
} finally {
fis.close();
}
XmlToJson xmlToJson = new XmlToJson.Builder(contents.toString()).build();
// convert to a JSONObject
JSONObject jsonObject = xmlToJson.toJson();
// OR convert to a Json String
String jsonString = xmlToJson.toString();
// OR convert to a formatted Json String (with indent & line breaks)
String formatted = xmlToJson.toFormattedString();
Log.e("xml",contents.toString());
Log.e("json",jsonObject.toString());
} catch (Exception e) {
displayMessage("Error: " + e.getMessage());
}
}
public void displayMessage( String text )
{
tv.post( new MessagePoster( text ) );
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_results, menu);
return true;
}
class MessagePoster implements Runnable {
public MessagePoster( String message )
{
_message = message;
}
public void run() {
tv.append( _message + "\n" );
setContentView( tv );
}
private final String _message;
}
}
I followed this link : https://github.com/smart-fun/XmlToJson
Can I only parse xml? How can I get the data out of xml string?
Following is the xml string:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd" xmlns="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd">
<businessCard imageRotation="noRotation">
<field type="Mobile">
<value>•32147976</value>
</field>
<field type="Address">
<value>Timing: 11:00 a.m. to 5.00 p.m</value>
</field>
<field type="Address">
<value>MULTOWECIALITY HOSPITAL Havnmg Hotel MwyantfwfMf), TOL: 1814 7»7» / 0454 7575 fax: 2514 MSS MtoMte t wvHwJaMtur0Mapttal.com</value>
</field>
<field type="Name">
<value>M. S. (Surgery), Fais, Fics</value>
</field>
<field type="Company">
<value>KASTURI MEDICARE PVT. LTD.</value>
</field>
<field type="Job">
<value>Consulting General Surgeon Special Interest: Medical Administrator: KsturiSecretary: IMA - Mira</value>
</field>
<field type="Text">
<value>Mob.: •32114976
Dr. Rakhi R
M. S. (Surgery), Surgeon
Special Interest: Medical
President: Bhayander Medical Association
Scientific Secretary: IMA - Mira Bhayander
Timing: 11:00 a.m. to 5.00 p.m
%
*
KASTURI MEDICARE PVT. LTD.
ISO 9001:2008 Certified, ASNH Cliniq 21 Certified,
MtoMte t wvHwJaMtur0Mapttal.com
mkhLkasturi0gmoiH.com</value>
</field>
</businessCard>
I checked this link to parse the xml: http://androidexample.com/XML_Parsing_-_Android_Example/index.php?view=article_discription&aid=69
But this string dose not have the list, I am not getting how to parse this xml string. Can anyone help please?? Thank you..
You can parse Json easily than XML.
So I will suggest you to parse Json,
First Convert XMLto Json then parse the JsonObject.
here is reference you can take to convert XML to JSON Step by Step
https://stackoverflow.com/a/18339178/6676466
For Xml parsing you can go for either XML Pull Parser or XML DOM Parser.
Both the process are quite lengthy and involves a lot code as it focuses on manual parsing on XML.
Another way is to use This Library in your project and boom most of your job is done. It will parse your XML just like you parse your JSON using GSON.
All you need to do is to create a instance of the parser and use it like:
XmlParserCreator parserCreator = new XmlParserCreator() {
#Override
public XmlPullParser createParser() {
try {
return XmlPullParserFactory.newInstance().newPullParser();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
};
GsonXml gsonXml = new GsonXmlBuilder()
.setXmlParserCreator(parserCreator)
.create();
String xml = "<model><name>my name</name><description>my description</description></model>";
SimpleModel model = gsonXml.fromXml(xml, SimpleModel.class);
Remember that you need to create a POJO class for your response just like you do for GSON.
Include the library in your gradle using:
compile 'com.stanfy:gson-xml-java:0.1.+'
Please read the github link for library carefully to know the usage and limitations.
from your question I don't get the reason to convert xml to json but just to get a way to fetch some fields out of the xml directly.
If there is no need to process the json data at a later step I recommend you to use XPATH. With Xpath you can get the data of you xml with a simple path query like "/document/businessCard/field[#type='Mobile']/value"
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(URI_TO_YOUR_DOCUMENT);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("/document/businessCard/field[#type='Mobile']/value");
We are parsing an XML file with the SAX parser. Is it possible to get the schema location from the XML?
<view id="..." title="..."
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="{schema}">
I want to retrieve the {schema} value from the XML. Is this possible? And how to I access this value of noNamespaceSchemaLocation? I'm using the default SAX Parser.
#Override
public void startElement(String uri, String localName,
String name, Attributes attributes)
{ .... }
Thank you.
It all depends with what kind of tool/library you are working (a basic SAXParser? Xerces? JDom? ...) But what you want is the value of the attribute "noNamespaceSchemaLocation" in the namspace defined by the URI "http://www.w3.org/2001/XMLSchema-instance"
in JDom, it would be something like:
Element view = ...; // get the view element
String value = view.getAttributeValue("noNamespaceSchemaLocation", Namespace.getNamespace("http://www.w3.org/2001/XMLSchema-instance"));
Here is how I get the XSD's name using XMLStreamReader:
public static String extractXsdValueOrNull(#NonNull final InputStream xmlInput)
{
final XMLInputFactory f = XMLInputFactory.newInstance();
try
{
final XMLStreamReader r = f.createXMLStreamReader(xmlInput);
while (r.hasNext())
{
final int eventType = r.next();
if (XMLStreamReader.START_ELEMENT == eventType)
{
for (int i = 0; i <= r.getAttributeCount(); i++)
{
final boolean foundSchemaNameSpace = XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI.equals(r.getAttributeNamespace(i));
final boolean foundLocationAttributeName = SCHEMA_LOCATION.equals(r.getAttributeLocalName(i));
if (foundSchemaNameSpace && foundLocationAttributeName)
{
return r.getAttributeValue(i);
}
}
return null; // only checked the first element
}
}
return null;
}
catch (final XMLStreamException e)
{
throw new RuntimeException(e);
}
}
Actually XMLStreamReader does all the magic, namely:
only parses the XML's beginning (not the whole XML)
does not assume a particular namespace alias (i.e. xsi)
I want to create/add a custom ID3 tag to a MP3 (ID3v2.3 or ID3v2.4). There is a TXXX tag for this purpose, but I don't know how to create it using the library jAudiotagger.
Just found out myself, the following code is not properly tested / not clean but does the task:
/**
* This will write a custom ID3 tag (TXXX).
* This works only with MP3 files (Flac with ID3-Tag not tested).
* #param description The description of the custom tag i.e. "catalognr"
* There can only be one custom TXXX tag with that description in one MP3 file
* #param text The actual text to be written into the new tag field
* #return True if the tag has been properly written, false otherwise
*/
public boolean setCustomTag(AudioFile audioFile, String description, String text){
FrameBodyTXXX txxxBody = new FrameBodyTXXX();
txxxBody.setDescription(description);
txxxBody.setText(text);
// Get the tag from the audio file
// If there is no ID3Tag create an ID3v2.3 tag
Tag tag = audioFile.getTagOrCreateAndSetDefault();
// If there is only a ID3v1 tag, copy data into new ID3v2.3 tag
if(!(tag instanceof ID3v23Tag || tag instanceof ID3v24Tag)){
Tag newTagV23 = null;
if(tag instanceof ID3v1Tag){
newTagV23 = new ID3v23Tag((ID3v1Tag)audioFile.getTag()); // Copy old tag data
}
if(tag instanceof ID3v22Tag){
newTagV23 = new ID3v23Tag((ID3v11Tag)audioFile.getTag()); // Copy old tag data
}
audioFile.setTag(newTagV23);
}
AbstractID3v2Frame frame = null;
if(tag instanceof ID3v23Tag){
frame = new ID3v23Frame("TXXX");
}
else if(tag instanceof ID3v24Tag){
frame = new ID3v24Frame("TXXX");
}
frame.setBody(txxxBody);
try {
tag.addField(frame);
} catch (FieldDataInvalidException e) {
e.printStackTrace();
return false;
}
try {
audioFile.commit();
} catch (CannotWriteException e) {
e.printStackTrace();
return false;
}
return true;
}