EzDevInfo.com

parsing interview questions

Top parsing frequently asked interview questions

Parse JSON in JavaScript? [duplicate]

This question already has an answer here:

I want to parse a JSON string in JavaScript. The response is something like

var response = '{"result":true,"count":1}';

How can I get the values result and count from this?


Source: (StackOverflow)

In Java, how do I parse XML as a String instead of a file?

I have the following code:

DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlFile);

How can I get it to parse XML contained within a String instead of a file?


Source: (StackOverflow)

Advertisements

How to join multiple lines of file names into one with custom delimiter?

I would like to join the result of ls -1 into one line and delimit it with whatever i want.

Are there any standard Linux commands I can use to achieve this?


Source: (StackOverflow)

Sending and Parsing JSON Objects

I would like to send messages in the form of JSON objects to a server and parse the JSON response from the server.

I thought of this format for the response from the server:

{
  "post": {
    "username": "someusername",
    "message": "this is a sweet message",
    "image": "http://localhost/someimage.jpg",
    "time":  "present time"
  }
}

How much knowledge of JSON should I have to accomplish this purpose? Also it would be great if someone could provide me links of some tutorials for sending and parsing JSON Objects.


Source: (StackOverflow)

What is the opposite of 'parse'? [closed]

I have a function, parseQuery, that parses a SQL query into an abstract representation of that query.

I'm about to write a function that takes an abstract representation of a query and returns a SQL query string.

What should I call the second function?


Source: (StackOverflow)

Regular expression pattern not matching anywhere in string

I am trying to match <input> type “hidden” fields using this pattern:

/<input type="hidden" name="([^"]*?)" value="([^"]*?)" />/

This is sample form data:

<input type="hidden" name="SaveRequired" value="False" /><input type="hidden" name="__VIEWSTATE1" value="1H4sIAAtzrkX7QfL5VEGj6nGi+nP" /><input type="hidden" name="__VIEWSTATE2" value="0351118MK" /><input type="hidden" name="__VIEWSTATE3" value="ZVVV91yjY" /><input type="hidden" name="__VIEWSTATE0" value="3" /><input type="hidden" name="__VIEWSTATE" value="" /><input type="hidden" name="__VIEWSTATE" value="" />

But I am not sure that the type, name, and value attributes will always appear in the same order. If the type attribute comes last, the match will fail because in my pattern it’s at the start.

Question:
How can I change my pattern so it will match regardless of the positions of the attributes in the <input> tag?

P.S.: By the way I am using the Adobe Air based RegEx Desktop Tool for testing regular expressions.


Source: (StackOverflow)

Best way to compare 2 XML documents in Java

I'm trying to write an automated test of an application that basically translates a custom message format into an XML message and sends it out the other end. I've got a good set of input/output message pairs so all I need to do is send the input messages in and listen for the XML message to come out the other end.

When it comes time to compare the actual output to the expected output I'm running into some problems. My first thought was just to do string comparisons on the expected and actual messages. This doens't work very well because the example data we have isn't always formatted consistently and there are often times different aliases used for the XML namespace (and sometimes namespaces aren't used at all.)

I know I can parse both strings and then walk through each element and compare them myself and this wouldn't be too difficult to do, but I get the feeling there's a better way or a library I could leverage.

So, boiled down, the question is:

Given two Java Strings which both contain valid XML how would you go about determining if they are semantically equivalent? Bonus points if you have a way to determine what the differences are.


Source: (StackOverflow)

What are the pros and cons of the leading Java HTML parsers? [closed]

Searching SO and Google, I've found that there are a few Java HTML parsers which are consistently recommended by various parties. Unfortunately it's hard to find any information on the strengths and weaknesses of the various libraries. I'm hoping that some people have spent some comparing these libraries, and can share what they've learned.

Here's what I've seen:

And if there's a major parser that I've missed, I'd love to hear about its pros and cons as well.

Thanks!


Source: (StackOverflow)

How to read XML using XPath in Java

I want to read XML data using XPath in Java, so for the information I have gathered I am not able to parse XML according to my requirement.

here is what I want to do:

Get XML file from online via its URL, then use XPath to parse it, I want to create two methods in it. One is in which I enter a specific node attribute id, and I get all the child nodes as result, and second is suppose I just want to get a specific child node value only

<?xml version="1.0"?>
<howto>
  <topic name="Java">
      <url>http://www.rgagnonjavahowto.htm</url>
  <car>taxi</car>
  </topic>
  <topic name="PowerBuilder">
       <url>http://www.rgagnon/pbhowto.htm</url>
       <url>http://www.rgagnon/pbhowtonew.htm</url>
  </topic>
  <topic name="Javascript">
        <url>http://www.rgagnon/jshowto.htm</url>
  </topic>
 <topic name="VBScript">
       <url>http://www.rgagnon/vbshowto.htm</url>
 </topic>
 </howto>

In above example I want to read all the elements if I search via @name and also one function in which I just want the url from @name 'Javascript' only return one node element.

I hope I cleared my question :)

Thanks.

Kai


Source: (StackOverflow)

C#: Test if string is a guid without throwing exceptions?

I want to try to convert a string to a Guid, but I don't want to rely on catching exceptions (

  • for performance reasons - exceptions are expensive
  • for usability reasons - the debugger pops up
  • for design reasons - the expected is not exceptional

In other words the code:

public static Boolean TryStrToGuid(String s, out Guid value)
{
    try
    {
        value = new Guid(s);
        return true;
    }
    catch (FormatException)
    {
        value = Guid.Empty;
        return false;
    }
}

is not suitable.

I would try using RegEx, but since the guid can be parenthesis wrapped, brace wrapped, none wrapped, makes it hard.

Additionally, I thought certain Guid values are invalid(?)


Update 1

ChristianK had a good idea to catch only FormatException, rather than all. Changed the question's code sample to include suggestion.


Update 2

Why worry about thrown exceptions? Am I really expecting invalid GUIDs all that often?

The answer is yes. That is why I am using TryStrToGuid - I am expecting bad data.

Example 1 Namespace extensions can be specified by appending a GUID to a folder name. I might be parsing folder names, checking to see if the text after the final . is a GUID.

c:\Program Files
c:\Program Files.old
c:\Users
c:\Users.old
c:\UserManager.{CE7F5AA5-6832-43FE-BAE1-80D14CD8F666}
c:\Windows
c:\Windows.old

Example 2 I might be running a heavily used web-server wants to check the validity of some posted back data. I don't want invalid data tying up resources 2-3 orders of magnitude higher than it needs to be.

Example 3 I might be parsing a search expression entered by a user.

enter image description here

If they enter GUID's I want to process them specially (such as specifically searching for that object, or highlight and format that specific search term in the response text.)


Update 3 - Performance benchmarks

Test converting 10,000 good Guids, and 10,000 bad Guids.

Catch FormatException:
   10,000 good:     63,668 ticks
   10,000 bad:   6,435,609 ticks

Regex Pre-Screen with try-catch:
   10,000 good:    637,633 ticks
   10,000 bad:     717,894 ticks

COM Interop CLSIDFromString
   10,000 good:    126,120 ticks
   10,000 bad:      23,134 ticks

p.s. I shouldn't have to justify a question.


Source: (StackOverflow)

lexers vs parsers

Are lexers and parsers really that different in theory ?

It seems fashionable to hate regular expressions: coding horror, another blog post.

However, popular lexing based tools: pygments, geshi, or prettify, all use regular expressions. They seem to lex anything...

When is lexing enough, when do you need EBNF ?

Has anyone used the tokens produced by these lexers with bison or antlr parser generators?


Source: (StackOverflow)

void ( *( *f[] ) () ) ()

I just saw a picture today and think I'd appreciate explanations. So here is the picture:

some c code

I found this confusing and wondered if such codes are ever practical. I googled the picture and found another picture in this reddit entry, and here is that picture:

some interesting explanation

So this "reading spirally" is something valid? Is this how C compilers parse?
It'd be great if there are simpler explanations for this weird code.
Apart from all, can these kind of codes be useful? If so, where and when?

There is a question about "spiral rule", but I'm not just asking about how it's applied or how expressions are read with that rule. I'm questioning usage of such expressions and spiral rule's validity as well. Regarding these, some nice answers are already posted.


Source: (StackOverflow)

How to convert string into float in javascript?

I am trying to parse two values from a datagrid.
The fields are numeric, and when they have a comma (ex. 554,20), I can't get the numbers after the comma.
I've tried parseInt and parseFloat. How can I do this?


Source: (StackOverflow)

How to parse a string to an int in C++?

What's the C++ way of parsing a string (given as char *) into an int? Robust and clear error handling is a plus (instead of returning zero).


Source: (StackOverflow)

How to parse a query string into a NameValueCollection in .NET

I would like to parse a string such as "p1=6&p2=7&p3=8" into a NameValueCollection.

What is the most elegant way of doing this when you don't have access to the Page.Request object?


Source: (StackOverflow)